Access to fields in IF sentence in DAX in Power BI Desktop - powerbi

When I use SUM for example the intellisense of the editor shows me the columns of my table but when I use IF or Switch I'm not shown any column.
In this example of If (https://powerbi.microsoft.com/es-es/documentation/powerbi-desktop-tutorial-create-calculated-columns/) the column between [] works fine but when I put my column between [] I have error
error if
Any idea please?
Regards

I can see you are creating a measure while in the tutorial they are creating a calculated column. Measures and Calculated columns behave different, while creating a calculated column the expression you are using takes the context of each row so you can reference any column directly by [Column].
However measures evaluate in different context so they need an aggregation function to determine the value of your columns.
EXAMPLE: Calculated Column.
Is Red Calculated Column = IF([Color]="Red","Yes","No")
EXAMPLE: Measures.
Is Red Measure = IF(FIRSTNONBLANK([Color],0)="Red","Yes","No")
Note I used FIRSTNONBLANK aggregation function to access the [Color] column from a measure.
The above expression could not work for you depending on your measure, I only posted it for example purposes.

I found the answer
TotalHoras = SUMX(Horas;if(Horas[Tipo]="M";Horas[Duration]/60;Horas[Duration]))
Like #alejandrozuleta says Measures need an aggregation function to works

Related

Why CALCULATE is not modifying filter context when used with FILTER?

I have a table with 2 measures - One and Two. Both uses CALCULATE and have a filter. There's also a slicer. While Two rightfully ignores slicer's filter context, One doesn't.
'Years'[Year] is a Whole number.
One = CALCULATE(SUM(Years[Sales Amount]),FILTER(Years, 'Years'[Year] = 2010))
Two = CALCULATE(SUM(Years[Sales Amount]),'Years'[Year] = 2010)
What am I missing (in my learning)? I understand Microsoft recommends to NOT use FILTER as filter argument. I'm merely trying to get a grip on the way it works.
There are some tricky details on CALCULATE that produce different results. I’ll show the differences and provide examples below.
Measure One
One = CALCULATE(SUM(Years[Sales Amount]),FILTER(Years, 'Years'[Year] = 2010))
First, remember that every language calculates from the most internal to the most external function.
It means that Power BI will first calculate the function FILTER, which will return a table that contains only the year 2010. Only after that Power BI will perform CALCULATE.
A tricky part here: If there is a filter in the 2nd parameter, the CALCULATE can ignore the slicer (more details on measure two). However, there is no filter on the 2nd parameter, there is only a table! (previously calculated, which contains only year 2010 now).
Since there is no filter, CALCULATE applies the context filter (the slicer in your example) excluding anything before 2013.
Given that, no rows match the filters for Measure One and the result will be blank (if the slicer is set to 2013-2018, of course).
Measure Two
Two = CALCULATE(SUM(Years[Sales Amount]),'Years'[Year] = 2010)
Measure Two do have a filter expression: 'Years'[Year] = 2010
When filter expressions are provided, the CALCULATE function changes the filter context (which contains slicers or filter on page etc.) to evaluate the expression.
For each filter expression, there are two potential outcomes in CALCULATE:
Outcome A. If the columns are already in the filter context, the existing filters will be overwritten by the parameter in the CALCULATE expression. I.e., does not matter the slicer (or filter on this page or on all pages…)
That is the case for your measure two!
Since you are using 'Years'[Year] in both the CALCULATE expression and the slicer, the context filter will be overwritten by the filter in the CALCULATE expression. I mean, the result is ignoring the slicer/filter context and you will find "3" as the result.
Outcome B. If the columns aren't in the filter context, then new filters will be added to the filter context to evaluate the expression. In other words, it will consider both the slicer/filter context and the filter expression in CALCULATE.
To exemplify this behavior, I have created a new measure:
ThreeSalesFilter = CALCULATE(SUM(Years[Sales Amount]), 'Years'[Sales Amount]=3)
It keeps the exact same row as measure two, right? The Year=2010 is the same row Sales Amount=3.
However, the result of this measure is blank and not 3. Quite interesting!
Again, it happens because CALCULATE is using 'Years'[Sales Amount] but the slicer/filter context is using 'Years'[Year] – another column. Therefore the CALCULATE filter "joins" the filter context; do not overwrite it.
Below, the reproduction of the dashboard including my third variable.
If you need more details or clarifications, please let me know in the comments and I'll edit here.
You are getting different behaviors because of the way that the CALCULATE function works with filter context; you are passing two different types of arguments into the calculate function.
In measure one you are passing a table filter expression. It takes the table created by the FILTER function, and then applies the filter context given by the slicer. Table filter expressions do not override the existing filters on any columns.
In measure two you are passing a Boolean filter expression as the second argument. It overrides the filter context since they both filter based on the year column, therefore ignoring the slicer.
MS best practices do caution against using FILTER as a filter argument, but they do also mention examples of several times where it is appropriate.
I try to create a table like yours
also create the two measures which build on the dashboard
So, I think the values displayed by One and TWO are not the same as the DAX syntax. But your One is BLANK that the possible reason is that One caught the filter condition on the right side of your way, while TWO caught the value of the table on the left, I guess it is because it's not the same.

Power BI Visuals with Measures only without summing up

Situation:
My Report calculates the ranking developments of an accounts in one measure:
Class_Evolution = [Class_Minus_0] & " <-- " & [Class_Minus_1]
Combined with data from the source table the measures show good results per datarow.
The results look like:
...
[Class_Minus_0] and [Class_Minus_1] are measures with X-functions themself that result in a ranking (A,B,C,D) depending on slicer selection.
I also have a measure that counts the rows:
Count Values = COUNTROWS(ExhibitorClass)
This works so far.
Problem:
Now I need to crate visuals with the measures I created. But when I put my measures on a visual they just get summed up:
Need:
I need to built the visuals as in the example below but by using my measured instead of columns.
I have build the report without a slicer on fixed data columns with fixed ranking cutoffs and got a nice result:
However I need to be able to calculate the ranking development with the slicer so I need to build everything with measures.
How should I build my measures to get the visuals I need? Please help me.
Thanks to Andrew, I was able to implement the following solution.
I needed a new Table matching the possible results of the measure:
Then with a VAR variable in a DAX measure the matching values could be counted:
Visual Count Class Evolution =
VAR _rank_evolution = SELECTEDVALUE('Class_Evolution'[Class_Evolution])
return sumx('ExhibitorClass_Details', if([Class_Evolution] = _rank_evolution, 1,0))
The variable was populated form my [Class_Evolution] table and if the measure used on my [ExhibitorClass_Details] details table matched the Class_Evolution it was summed up.

same DAX query resulting in different results

For a table (having 2 rows and 3 columns namely City1,Price1 & Units1) in power bi, I created a new column (named 'salesColumn') and a new measure (named 'salesMeasure) with the below DAX queries
salesColumn = SUMX('Table', 'Table'[Price1] * 'Table'[Units1])
salesMeasure = SUMX('Table', 'Table'[Price1] * 'Table'[Units1])
This resulted in the following visual:
As seen above, formula for both the column & measure are same but still they give different results. While salesColumn gives 6572 (12*175 + 18*229) in both rows, salesMeasure gives the row wise product. Can anyone please let me know why the behaviour is different in the two cases while we are using the same formula ? SUMX is a function to which we are passing the same arguments. If we are passing same arguments to a function, the function should behave in the same manner, but it is not so. Is there any hidden argument being passed to SUMX ? Can anyone please explain what is happening here & possibly paste some relevant links to understand this better ?
This Two calculation should give you different output, the reason is that the SaleColumn are calculated only once at refresh time (The DAX expression defined for a calculated column operates in the context of the current row across that table.) --> you calculate a value for all rows (look at the summary from salesMeasure), while SaleMeasure is dynamic.
Even if they look similar, there is a big difference between
calculated columns and measures. The value of a calculated column is
computed during data refresh and uses the current row as a context; it
does not depend on user interaction in the report. A measure operates
on aggregations of data defined by the current context, which depends
on the filter applied in the report – such as slicer, rows, and
columns selection in a pivot table, or axes and filters applied to a
chart.
https://www.sqlbi.com/articles/calculated-columns-and-measures-in-dax/

When to use calculated field vs measure in Power BI?

Power BI allows to add calculated field and measure to table. Both create new column and allow me to add DAX formula.
When to use calculated field vs measure in Power BI?
The most important difference is that calculated columns are calculated once when the dataset is loaded. Their value does not change later, i.e. it is not affected by slicers for example. Measures are dynamic. They are calculated whenever necessary, thus they will respond to slicers in the report.
I would recommend to read this article - Measure vs Calculated Column: The Mysterious Question? Not!
Rule of thumb: If you want to use it in a filter or a slicer, put it in a column. Otherwise, you can create it as a measure.
Link: https://learn.microsoft.com/en-gb/learn/modules/dax-power-bi-add-measures/5-compare-calculated-columns-measures
Regarding similarities between calculated columns and measures, both are:
Calculations that you can add to your data model.
Defined by using a DAX formula.
Referenced in DAX formulas by enclosing their names within square brackets.
The areas where calculated columns and measures differ include:
Purpose - Calculated columns extend a table with a new column, while measures define how to summarize model data.
Evaluation - Calculated columns are evaluated by using row context at data refresh time, while measures are evaluated by using filter context at query time.
Storage - Calculated columns (in Import storage mode tables) store a value for each row in the table, but a measure never stores values in the model.
Visual use - Calculated columns (like any column) can be used to filter, group, or summarize (as an implicit measure), whereas measures are designed to summarize.

Create Multiple Calculated columns using one DAX formula

Is it possible to create 2 different columns using one DAX Expression?
I have 2 column, for example Work Done this month and Invoiced Amount. I want to create 2 columns using these.
Work Done - Invoiced and return only positive values (Deferred)
Invoiced - Work Done and return only positive values (Extra)
Note: I know how to add these columns using 2 DAX formula's here, but I would like to know if its possible with one formula.
Samsple Screenshot below:
I believe it is possible but not within the existing table and it strongly depends on the context on which your are calculating. When your calculation is performed on a row level, ADDCOLUMNS could help you out. It allows you to create a new table and add multiple calculated columns.
https://learn.microsoft.com/en-us/dax/addcolumns-function-dax