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])
)
Related
My intention is to populate days of the month to simulate a data warehouse periodic snapshot table using DAX measures. My goal is to show non-additive values for the quantity.
Consider the following transactions:
The granularity of my snapshot table is day. So it should show the following:
Take note that a day may have multiple entries but I am only interested in the latest entry for the day. If I am looking at the figures using a week period it should show the latest entry for the week. It all depends on the context fixter.
However after applying the measure I end up with:
There are three transactions. Two on day 2 and the other on day 4. Instead of calculating a running total I want to show the latest Qty for the days which have no transactions without running accumulating totals. So, day 4 should show 4 instead of summing up day 3 and day 4 which gives me 10. I've been experimenting with LASTNONBLANK without much success.
This is the measure I'm using:
Snapshot =
CALCULATE(
SUM('Inventory'[Quantity]),
FILTER(
ALL ( 'Date'[Date] ),
'Date'[Date] <= MAX( 'Date'[Date] )
)
)
There are two tables involved:
Table # 1: Inventory table containing the transactions. It includes the product id, the date/time the transaction was recorded and the quantity.
Table # 2: A date table 'Date' which has been marked as a date table in Power BI. There is a relationship between the Inventory and the Date table based on a date key. So, in the measure, 'Date'[Date] refers to the Date column in the Date table.
You can use the LASTNONBLANKVALUE function, that returns the last value of the expression specified as second parameter sorted by the column specified as first parameter.
Since LASTNONBLANKVALUE implicitly wraps the second parameter into a CALCULATE, a context transition happens and therefore the row context is transformed into the corresponding filter context. So we also need to use VALUES to apply the filter context to the T[Qty] column. The returned table is a single row column and DAX can automatically convert a single column, single row table to a scalar value.
Then, since we don't have a dimension table we have to get rid of cross-filtering, therefore we must use REMOVEFILTERS over the whole table.
the filter expression T[Day] < MaxDay is needed because LASTNONBLANKVALUE must be called in a filter context containing all the rows preceding and including the current one.
So, assuming that the table name is T with fields Day and Qty like in your sample data, this code should work
Edit: changed in order to support multiple rows with same day, assuming the desired result is the sum of the last day quantities
Measure =
VAR MaxDay =
MAX ( T[Day] )
RETURN
CALCULATE (
LASTNONBLANKVALUE (
T[Day],
SUM ( T[Qty] )
),
T[Day] <= MaxDay,
REMOVEFILTERS ( T )
) + 0
Edit: after reading the comments, this might work on your model (untested)
Measure =
VAR MaxDay =
MAX ( 'Date'[Date] )
RETURN
CALCULATE (
LASTNONBLANKVALUE (
Inventory[RecordedDate],
SUM ( Inventory[Quantity] )
),
'Date'[Date] <= MaxDay
) + 0
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 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.
I've been struggling to find a way to achieve the result of a matrix visualization in Power BI, in a line and clustered column chart.
I have tried using ALLSELECTED, ALL, ALLEXCEPT in order to ignore one filter but no matter how I try it, I'm not reaching my end goal.
CarCountPerCategory =
CALCULATE(
DISTINCTCOUNT(Table[CarID]),
ALLSELECTED(Table[Filter])
)
This still breaks down the measure to the Filter level instead of ignoring it. I am looking for something that will give me the total distinct count regardless of the Filter column. E.g. TimesReshopped 1 - 190 on all 3 columns, TimesReshopped 2 - 182 on all 3 columns of the Filter Column series. I need this as a sidestep in order to divide the distinct car count to the total distinct car count per times reshopped in order to reach the percentage that I am looking for. (the one in the matrix)
Can someone help me with some advice? Thank you.
Try this:
[Total Category Count]:=
SUMX ( SUMMARIZE ( Table, Table[TimesReshopped] ), [Count of CarID] )
I got to the bottom of this by using ALLEXCEPT and providing all the filter that I was using:
CarCountPerCategory =
CALCULATE(
DISTINCTCOUNT(Table[CarID]),
ALLEXCEPT(Table,Table[Year],Table[Month],Table[LocationName],Table[CustCode],Table[TimeReshoped])
)
I have a powerbi report which is running a dax formula to calculate a custom measure. In the picture below, the total at the bottom doesn't seem to add up to the individual rows. I've been trying my luck for some time and can't seem to figure out why this is.
The DAX formula used is as follows
SumRest<24hrs7Day = CALCULATE(
DISTINCTCOUNT(WorkTimeDirective[EmployeeKey]),
FILTER(
ADDCOLUMNS(
SUMMARIZE(WorkTimeDirective,Employee[EmployeeKey],'Date'[DateKey]),
"totRestHrs", CALCULATE(MAX(WorkTimeDirective[RestHours])
,DATESINPERIOD('Date'[DateKey], LASTDATE('Date'[DateKey]), -7, DAY))
),
[totRestHrs]<24
),
WorkTimeDirective[IsEmployeeAbove18]=1
)
Any idea why this is and what I am doing wrong.
For using SUMX the main step is listing the values which you are iterating over, which it typically a table or column. In this case it sounds like you would do a column.
For the example I just had it call the measure you already defined, since breaking DAX calculations into smaller pieces makes writing/testing complex formulas easier.
The idea being that it would iterate over the unique values which are in your TableName[Site Name], then run the [SumRest<24hrs7Day] under that context. I used TableName for the table due to not knowing the name for the table.
SUMX_Example = SUMX( VALUES( TableName[Site Name], [SumRest<24hrs7Day])