I have a measure that retrieves the latest bundle string from a column called Message.
The measure works by getting the first single row from SessionEvents and using MAXX to retrieve the Message from that row.
lastBundle =
VAR SINGLE_ROW = TOPN(1,, FILTER(SessionEvents, SessionEvents[StatusId]=4),
SessionEvents[DateTime],DESC)
return MAXX(SINGLE_ROW, [Message])
I want to use this measure in a calculated column but MAXX is not allowed as a part of a calculated column.
How should i change this calculation to work in a calculated column.
You can use SELECTCOLUMNS instead of MAXX as explained more fully in this related Q&A:
Return top value ordered by another column
lastBundle =
SELECTCOLUMNS (
TOPN (
1,
FILTER ( SessionEvents, SessionEvents[StatusId] = 4 ),
SessionEvents[DateTime], DESC
),
"Message", SessionEvents[Message]
)
But I don't see any reason why MAXX shouldn't work too, except your TOPN function has an extra comma.
Edit: I don't think you can do this at all if you are using a DirectQuery. From Microsoft's documentation:
Limitations in calculated columns: Calculated columns are limited to being intra-row, as in, they can only refer to values of other columns of the same table, without the use of any aggregate functions. [...] Functions that are not supported will not be listed in autocomplete when authoring the DAX for a calculated column, and would result in an error if used.
Related
This is my first question here.
This time I want to count values that appear in different columns. Each one corresponds to the values from a row, but there is only 1 column that they have in common (not shown in the picture). I need a measure to show a count of each word described in those cells.
For example, in this case (please ignore blanks, it's a test), the measure should give a count for the word 'Desodorante' as 3, 'Cabello' as 2, and the rest 1. These words are pre defined and there are no random values accepted.
I may say that I want to state each of these words as a kind of category. I would like to make a slicer out of them too.
example
I believe a workaround is to create a calculated table, stating as columns each of these values and allocating a count of each value from these 4 columns?
A Power Query solution to this problem has been posted here: PowerBI - Count instances of string in multiple columns
A DAX solution to this problem would be to create a new calculated table by appending the 4 columns using UNION() and SELECTEDCOLUMN() and then getting the count via SUMARIZE():
The 2 calculated Tables:
Mesa =
FILTER(
UNION(
SELECTCOLUMNS(
'Ramiro',
"Producto", 'Ramiro'[Producto]
),
SELECTCOLUMNS(
'Ramiro',
"Producto", 'Ramiro'[Producto2]
),
SELECTCOLUMNS(
'Ramiro',
"Producto", 'Ramiro'[Producto3]
),
SELECTCOLUMNS(
'Ramiro',
"Producto", 'Ramiro'[Producto4]
)
),
[Producto] <> BLANK()
)
Producto Count =
SUMMARIZE(
'Mesa',
'Mesa'[Producto],
"Count", COUNT('Mesa'[Producto])
)
I want to calculate the running count of each value based on column A. In Excel, I am applying the following formula
=COUNTIF($A$2:$A2,A2)
I would like to get the same result in Power BI. Can you please advise.
With just a single column, this is impossible in DAX because duplicated rows cannot be distinguished as there is no inherent order to a column.
However, if you have an index column on the table (you can easily add one in the Query Editor), then it is possible to define such a calculated column so that it works similarly to the Excel formula.
CountIf =
VAR CurrentIndex = DATA[Index]
RETURN
CALCULATE (
COUNTROWS ( DATA ),
ALLEXCEPT ( DATA, DATA[ITEM] ),
DATA[Index] <= CurrentIndex
)
This works:
EVALUATE
ADDCOLUMNS(
FILTER (Sales, [StoreKey] = 155 ) ,
"Margin", (Sales[SalesAmount] - Sales[TotalCost])
)
However, if I try to define a function, I get an error:
DEFINE VAR Margin = Sales[SalesAmount] - Sales[TotalCost]
EVALUATE
ADDCOLUMNS(
FILTER (Sales, [StoreKey] = 155) ,
"Margin", Margin
)
Query (1, 21) A single value for column 'SalesAmount' in table 'Sales'
cannot be determined. This can happen when a measure formula refers to
a column that contains many values without specifying an aggregation
such as min, max, count, or sum to get a single result.
What is this error and how to fix it?
There are 2 problems with the second code:
It's missing Row Context
VAR is not a function, it's a variable. Functions in DAX are Measures
What is "Row Context"? In short, in Power BI data is stored in a database. When you are referring to a column in the database, you must either: aggregate the data in it (i.e., sum it), or provide a specific row ("row context").
In your first code, function ADDCOLUMNS is an iterator. It means that it loops a table row by row, and for each record does a calculation. Since during each iteration it "knows" which row it's on, you can refer to the table fields without problems.
In the second code, this line:
Margin = Sales[SalesAmount] - Sales[TotalCost]
has no row context - it does not know which record to use for the calculation, and hence the error. You must either aggregate the data first, or put this calculation inside an iterator.
In this particular case, the simplest solution is to use aggregation:
DEFINE
MEASURE 'Sales'Margin = SUM ( Sales[SalesAmount] ) - SUM ( Sales[TotalCost] )
EVALUATE
ADDCOLUMNS (
CALCULATETABLE ( Sales, Sales[StoreKey] = 155 ),
"Margin", [Margin]
)
Here, we first aggregate amounts and costs and calculate margin. Then, inside of ADDCOLUMNS we iterate table Sales filtered for a specific store, and for each row we call the measure defined above.
If you need to use an iterator instead of aggregation, you can do something like this:
DEFINE
MEASURE 'Sales'Margin = SUMX(Sales, Sales[SalesAmount] - Sales[TotalCost] )
EVALUATE
ADDCOLUMNS (
CALCULATETABLE ( Sales, Sales[StoreKey] = 155 ),
"Margin", [Margin]
)
I have a slicer, called COUNTRY and applied to table MY_TABLE. When I calculate a measure, everything works as expected:
-- calculates distinct count only for COUNTRY = x
Some Measure = DISTINCTCOUNT('MY_TABLE'[SOME_COLUMN])
The problem is SUMMARIZE ignores slicer selection:
-- calculates distinct count accross all countries: x, y, z, etc.
Calculated Table =
RETURN SUMMARIZE(
'SOME_TABLE',
[CATEGORY],
"COUNT", DISTINCTCOUNT('SOME_TABLE'[SOME_COLUMN])
)
How to make SUMMARIZE take into account slicers?
Only Measures are "responsive", calculated tables and columns get calculated and created once, when the data are loaded.
Note that if a calculated table is used inside a measure it will behave correctly, but as you may know, a measure must return a scalar value and not a table. (ie you can use summarize inside a measure, you can then filter the obtained table and return the sum of one column)
Of course, you can filter calculated table with a slicer. If you can, go for SUMMARIZECOLUMNS because this function is better optimized then SUMMARIZE, and has arguments for filtering.
Filtering SUMMARIZECOLUMNS
If you want to stick to SUMMARIZE, you can filter your table by wrapping it with CALCULATETABLE.
Calculated Table =
CALCULATETABLE (
SUMMARIZE (
'SOME_TABLE',
[CATEGORY],
"COUNT", DISTINCTCOUNT ( 'SOME_TABLE'[SOME_COLUMN] )
),
Dim[Color]
= SELECTEDVALUE ( Slicer[SlicerValues] )
)
Should FILTER be used inside or outside of SUMMARIZE?
Within Power BI Desktop (Version: 2.39.4526.362 64-bit (September 2016), I have written a DAX Statement that behaves differently when the column is sorted by another vs when it is not sorted by another.
Measure:
Sum of Sales Across All Months =
CALCULATE ( SUM ( SalesAmount ), ALL ( 'Date'[MonthName] ) )
When the MonthName column is unsorted by another column, the measure behaves as I expect. Eliminating the filter context of the MonthName column. However, as soon as I set the MonthName column to be sorted by another column (e.g., MonthNumber) the "ALL" context reset is lost and it reverts back to the MonthName context.
Does anyone know if this is a bug or if I am misunderstanding something?
Thanks!
When one column sorts by another column, the DAX that Power BI generates includes the sort-by column, even though it's not visible in your visual. Therefore, for the measure to behave as you'd expect, you need to remove the filter context from both columns, even though only one is visible:
Sum of Sales Across All Months =
CALCULATE (
SUM ( SalesAmount ),
ALL ( 'Date'[MonthName] ),
ALL ( 'Date'[MonthNumber] )
)
It's unintuitive, but I don't know that it's a bug. There's a blog post that describes the behaviour you're seeing here: https://blog.crossjoin.co.uk/2015/12/15/power-bi-desktop-sort-by-column-and-dax-calculations-that-use-the-all-function/