Categories
Hints & Tips Power Apps Power BI Technology

Power BI DAX – the mysterious Calculate function 4 – tricks

DAX (Data Analysis eXpressions) is the function language in Power BI desktop. When DAX is used to create measures, it can hard to understand the logic at times. Especially one piece, the Calculate function, can be challenging.

 

This is the fourth part of a series of blog posts, which will investigate the mysterious Calculate function.

The Calculate function react differently to row, column, filter, and table context than the other DAX functions and can be used with date intelligence and can handle many other special requirements.

 

Trick the data model to Pass Filters from Many to One Side of Relationship

In this blog post you will see how the Calculate function can trick the data model to Pass Filters from Many to One Side of Relationship.

In a report the number of cities you have sold to need to be visualised.

A DistinctCount function can count distinct entries. However, as you can see below, the DistinctCount function cannot filter the result by month. It just returns 49, which are the total number of cities sold to.

 

 

The reason that the DistinctCount function cannot filter it by month is clear in the data model.

 

It goes against the filter direction in the relationship between the Orders and the Customers table.

The Dates table is related to the Orders table, and the filter direction is going from the Dates table to the Orders table, but the filter direction in the relationship between Orders to the Customers table is going from the Customer table to the Orders table. This means that the Dates table cannot filter the cities.

Distinct Shipping Methods

The DistinctCount function can show the number of distinct shipping methods used each month. This is because the relationship’s filter direction between the Dates and Orders table is going from the Dates table to the Orders table.

The task was to count how many different cities sold to each month. So how are you going to achieve this?

The mysterious Calculate function can handle this, as it can handle a lot of other special tasks.

The DAX measure will look like this:

Distinct Cities = CALCULATE(DISTINCTCOUNT(Customers[City]),orders)

By nesting the DistinctCount function inside the Calculate function and adding the Orders table to a filter argument in the Calculate function, you now can pass filters from Many to One side of Relationship (go against the filter direction). You will need to enter the Calculate function into the expression to reference the table you want the expression from as well as in the filter argument the table, which are related to the table in the expression.

You can see above that now the measure returns the right result.

Distinct Products Sold

If you need to visualise the number of distinct products sold each month, you can use a similar approach.

Again, you will have to go against the filter direction (Pass Filters from Many to One Side of Relationship). Here the relationship between the Items table and the Line Items table.

Conclusion

I call the Calculate function the mother of all DAX functions. It is the most important DAX function (my personal opinion). But you need to understand how it reacts to row, column, filter, and table context, and can be used to handle different issues. As a result, you will get the best out of it.  Concluding in this blog which has shown how it can pass filters from Many to One Side of Relationship.

This is part 4 of a series of blog posts as mentioned at the top about the mysterious Calculate function in Power BI Dax.

 

Categories
Application Data Visualisation Power BI Technology

Create Field Parameters in Power BI

Why is Power BI taking over the world? Why are so many organisations currently implementing Power BI? Power BI is the most efficient tool to manage business intelligence in a world where the data sets are getting bigger and bigger. The importance of making quick and correct decisions is a key factor in running a successful business. Microsoft constantly improves Power BI.  In this blog, we will show you how to create Field Parameters in Power BI.

 

The ‘Creating a Field Parameter’ tool can create a dynamic visual in few seconds, increasing your productivity. This gives the audience interactive options to choose which information they want the visual to display. In the past, we needed to write DAX expressions to achieve this.

In the following example you will see how this is done by creating a table and writing DAX expressions. DAX (Data Analysis epression) is essential to all Power BI reporting and data modelling developers. STL has a one day DAX course for all who need to write DAX in Power BI desktop.

Field Parameters done with DAX

Firstly, you need to create a table. Click Enter Data from the Home tab. In the first column, enter the names of the measures you want to be able to select from a slicer. The slicer will change the visual. Add a second column to create an index (see image below). Name the table at the bottom of this dialog box.

Create Field Parameters in Power BI

Create a Slicer visual and add the Measure column from the created table to the field (see image below).

Create Field Parameters in Power BI

Now it is time to run the DAX measure. You can use the logical Switch function to return the measure selected from the slicer.

Create Field Parameters in Power BI

The logic in the DAX measure is in the image below. The Switch function will test the value selected from the slicer. If the index value is 1 (Sales), it returns the Sales measure, if the index value is 2 (Profit) it returns the Profit measure. Index value 3 (Sales Cost) returns the Sales Cost measure.

Create Field Parameters in Power BI
Creating a visual to show the selected measure from the slicer

The visual (a line chart here) header will not show the selected measure from the slicer, but you can create one by writing another DAX measure.

Visual Name=SelectedValue(slicer1[measure],”Sales”)

This measure will get the selected value (measure name) from the slicer. If nothing is selected from the slicer, it will return the alternate result ‘Sales’ by default.

Now you can use the measure to create a dynamic title for the visual. With the visual selected, you will find the title under the formatting options. Next to the title there is an fx button. When you press the fx button, you can use the measure to create a dynamic title.

Create Field Parameters in Power BI

 

Clicking fx will open the dialog box where you can select the measure which creates the dynamic visual header.

Create Field Parameters in Power BI

The visual will now display the selected value from the slicer as a title!

Create Field Parameters in Power BI

Now to do something similar with the new tool – Creating a field parameter

The new tool we got in the May version of Power BI is a preview feature but will soon be a permanent tool.

Firstly, you will have to enable the tool called Field parameters:

Create Field Parameters in Power BI

Then, to create a new field parameter, you will need to navigate to Modeling > New parameter > Fields

Create Field Parameters in Power BI

To build the parameter, you need to provide a name for the parameter and select the fields you want to use. You can select different dimensions (fields from different tables across your data model) or different measures. The example below starts with different dimensions.

Create Field Parameters in Power BI

From a slicer, select the fields you would like to filter on the visual. If you keep the tick in the bottom left corner, you will create a slicer with the dimensions.

The Field parameters feature will write DAX code to make the slicer and the data model understand what you want.

The DAX code for this example

Filter by dimensions = {

(“ClientName”, NAMEOF(‘tblClients'[ClientName]), 0),

(“Category”, NAMEOF(‘tblProducts'[Category]), 1),

(“SalesRep”, NAMEOF(‘tblSalesRep'[SalesRep]), 2)

}

The slicer will display the field names from your data model. If you want more user-friendly names from the slicer, you can amend the code.

Filter by dimensions = {

(“Client”, NAMEOF(‘tblClients'[ClientName]), 0),

(“Product Category”, NAMEOF(‘tblProducts'[Category]), 1),

(“Sales Rep”, NAMEOF(‘tblSalesRep'[SalesRep]), 2)

}

Adding or removing fields after creating the Field Parameters slicer

Filter by dimensions = {

(“Sales Rep”, NAMEOF(‘tblSalesRep’[SalesRep]), 0),

(“Product Category”, NAMEOF(‘tblProducts’[Category]), 1),

(“Client”, NAMEOF(‘tblClients’[ClientName]), 2),

(“Month”,NAMEOF(Dates[Month Number]),3)

}

In the example above, the Month Number field has been added by amending the DAX code. The slicer will now give the report’s audience the option to break down the visual by month.

Create Field Parameters in Power BI

Using the Field Parameter tool to break down visuals by measures

You can mix and match different measures and dimensions within the same parameter. For example, you can use this feature to create a dynamic table where the columns can be either measures or dimensions.

As shown below, when you use the new Field Parameter feature, the title of the visual will display the selected fields from the slicer. Again, you will not have to write a DAX measure to do this. It can be this easy to create Field Parameters in Power BI.

Create Field Parameters in Power BI

Using a field parameter to control visual properties

Once you have created the field parameters, you can use them to control the measures or dimensions used in a visual.

You can use the parameter in the field drop zones for a visual.

Create Field Parameters in Power BI

Conclusion

The new Field Parameter feature is an efficient way of creating very dynamic visuals. It reduces the number of pages needed for the report to give the audience proper business intelligence. This is, then, how to create Field Parameters in Power BI for your reports.

For more information, please see STL’s two-day Power BI Modelling, Visualisation and Publishing course.