Type.FunctionReturn

D

T

The M Code Behind the Power Query M function Type.FunctionReturn

In this article, we’ll take a closer look at the M code behind the Type.FunctionReturn function, exploring its syntax, behavior, and applications. We’ll also provide some practical examples of how this function can be used to enhance your data analysis and visualization workflows.

Understanding the Syntax of Type.FunctionReturn

Before we dive into the specific applications of Type.FunctionReturn, it’s important to have a solid understanding of its syntax. At its most basic level, Type.FunctionReturn is a type annotation function that allows you to specify the data type that a function should return.

The syntax for Type.FunctionReturn looks like this:


Type.FunctionReturn(returnType as type)


In this syntax, `returnType` is a required argument that specifies the data type that the function should return. This data type can be any valid Power Query data type, including text, numbers, dates, lists, tables, and more.

Enhancing Data Analysis with Type.FunctionReturn

One of the most powerful applications of the Type.FunctionReturn function is in enhancing your data analysis workflows. By specifying the data type of the output from a particular function, you can ensure that your data is properly structured and formatted for further analysis and visualization.

For example, let’s say that you have a dataset that contains information about sales transactions. You want to calculate the average sales revenue for each salesperson, and then visualize this data in a bar chart.

To do this, you might create a custom function that calculates the average sales revenue for a given salesperson. You can use the Type.FunctionReturn function to specify that this function should return a decimal number, like this:


let

averageSales = (salesperson as text) =>

let

salesData = Filter(Sales, Sales[Salesperson] = salesperson),

totalRevenue = List.Sum(salesData[Revenue]),

numTransactions = List.Count(salesData),

averageRevenue = totalRevenue / numTransactions

in

Type.FunctionReturn(averageRevenue, type number)

in

averageSales


In this example, we’re using Type.FunctionReturn to specify that the `averageRevenue` variable should be returned as a decimal number. This ensures that the data is properly formatted for further analysis and visualization, and makes it easier to work with in downstream workflows.

Transforming Data with Type.FunctionReturn

In addition to enhancing your data analysis workflows, the Type.FunctionReturn function can also be used to transform your data in powerful ways. By specifying the data type of the output from a particular function, you can ensure that your data is properly formatted and structured for your specific needs.

For example, let’s say that you have a dataset that contains information about customer orders. You want to create a new column that calculates the total revenue for each order, but you want this column to be formatted as currency.

To do this, you might create a custom function that calculates the total revenue for a given order. You can use the Type.FunctionReturn function to specify that this function should return a currency data type, like this:


let

calculateRevenue = (quantity as number, price as number) =>

let

revenue = quantity price

in

Type.FunctionReturn(revenue, type currency)

in

calculateRevenue


In this example, we’re using Type.FunctionReturn to specify that the `revenue` variable should be returned as a currency data type. This ensures that the data is properly formatted for downstream workflows, and makes it easier to work with in our data visualization and reporting tools.

The Type.FunctionReturn function is a powerful tool that can help you manipulate and transform your data in a variety of ways. By specifying the data type that a function should return, you can ensure that your data is properly formatted and structured for further analysis and visualization.

In this article, we’ve explored the syntax, behavior, and applications of the Type.FunctionReturn function in depth. We’ve provided some practical examples of how this function can be used to enhance your data analysis and visualization workflows, as well as transform your data in powerful ways.

With the right knowledge and tools at your disposal, you can leverage the full power of Power Query M to unlock insights and drive better business outcomes.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

    Subject

    Your Name (required)

    Company/Organisation

    Email (required)

    Telephone

    Training Course(s)

    Your Message

    Upload Example Document(s) (Zip multiple files)