PowerBI DAX get COUNT DISTINCT with GROUP BY , see SQL query below - powerbi

I have got this following SQL query that gives me the correct value from the database.
SELECT
SUM( DISTINCT_ORDER_NUMBERS )
FROM
(
SELECT STORE_KEY,
COUNT( DISTINCT TRANSACTION_NUM ) AS DISTINCT_ORDER_NUMBERS,
DATE_KEY,
TRANSACTION_TYPE_KEY
FROM Pos_Data
GROUP BY STORE_KEY,
DATE_KEY,
TRANSACTION_TYPE_KEY
)
AS A
I am however facing challenges writing a DAX formula for a measure in Power BI Here is what I have tried so far but I get an error.
Total Number Of Orders
VAR _TotalOrders =
SUMMARIZE('Pos_Data',
'Pos_Data'[STORE_KEY],
'Pos_Data'[DATE_KEY],
'Pos_Data'[TRANSACTION_TYPE_KEY],
"DISTINCT_ORDER_NUMBERS",
DISTINCTCOUNT('Pos_Data'[TRANSACTION_NUM]))
RETURN SUM(_TotalOrders[DISTINCT_ORDER_NUMBERS])
Please assist

The SUM function expects a base table rather than a calculated table.
Try this instead:
VAR _TotalOrders =
SUMMARIZE('Pos_Data',
'Pos_Data'[STORE_KEY],
'Pos_Data'[DATE_KEY],
'Pos_Data'[TRANSACTION_TYPE_KEY],
"DISTINCT_ORDER_NUMBERS",
DISTINCTCOUNT('Pos_Data'[TRANSACTION_NUM]))
RETURN SUMX(_TotalOrders, [DISTINCT_CHECK_SEQ])
Edit: If the difference you mentioned is related to nulls, then try this in place of DISTINCTCOUNT.
COUNTAX( DISTINCT( 'Pos_Data'[TRANSACTION_NUM] ), 'Pos_Data'[TRANSACTION_NUM] )
The COUNTAX function (as opposed to COUNTX) does not count nulls.

Related

DAX Flag in Matrix Visual - Multiple levels of analysis

I replicated an issue I am having with the 'Adventure Works DW 2020' pbix file, so if my analysis seems a little out of context, please understand this example is not the true data I am working with. The pbix I used can be downloaded here:
https://drive.google.com/file/d/1vn6CluiE5rrAF3UjYPh5ejb93H2JX6IX/view?usp=sharing
My goal is to create a measure that can flag the subset of records that I want to use for a matrix visual.
I created the following measure with notes in the syntax:
VAR TABLEVAR =
SELECTCOLUMNS(
FILTER(
SUMMARIZE(
CALCULATETABLE(Sales/*Apply several filters to Sales table*/
,NOT Sales[CustomerKey] = -1
,Sales[orderdatekey] > 20180731
,Sales[orderdatekey] < 20190601
)
,[CustomerKey]/*Count the number of products per customer*/
,"Count",COUNT(Sales[ProductKey])
)
,[Count] > 1/*Only keep customers that bought more than 1 product*/
)
,[CustomerKey] /*Select the identifiers of the desired customers*/
)
RETURN
{
SWITCH(TRUE()
,SELECTEDVALUE(Sales[CustomerKey]) IN TABLEVAR/*Flag the customers that were identified in the previous table*/
,1,BLANK()
)
}
Now, in the PowerBI Matrix visual, this seems to work at first:
I had successfully flagged the desired output. Now I just have to filter for the 'Analysis' measure to be 'Not Blank', but then this happens:
Now removing that filter and going down a level:
So you see, the measure does not evaluate at the record level of the table. Does anyone understand the concept I am missing here? I have tried all kinds of different measures but it all comes down to the same problem about flagging different levels of analysis.
Ideally, the output would only include the following(circled in green):
These are the records that are within the date filters I put into the CALCULATETABLE() arguments.
Any help or insight with this problem would be greatly appreciated. Thank you
I'm not 100% clear what you're trying to do but please try the following and see if it helps.
Analysis =
VAR TABLEVAR =
SELECTCOLUMNS(
FILTER(
SUMMARIZE(
CALCULATETABLE(Sales
,NOT Sales[CustomerKey] = -1
,Sales[orderdatekey] > 20180731
,Sales[orderdatekey] < 20190601,
REMOVEFILTERS()
)
,[CustomerKey]
,"Count",COUNT(Sales[ProductKey])
)
,[Count] > 1
)
,[CustomerKey]
)
RETURN
//CONCATENATEX(TABLEVAR, [CustomerKey], ",")
SWITCH(TRUE()
,SELECTEDVALUE(Sales[CustomerKey]) IN TABLEVAR
,1,BLANK()
)

Power BI - DAX - table variable - use column for further computation

I am using DAX language in Power BI Desktop.
I have a tricky situation where I am trying to use the column name generated from a table variable.
Table 1: SourceTable
Table 2: ReferenceTable
I need to develop a calculated column in SourceTable called EmploymentStatus, based on the corresponding column in ReferenceTable. But I need only the EmploymentStatus value from ReferenceTable, for the maximum InternalID for a given EmployeeEmail.
For example, for the email xyz.gmail.com in SourceTable, I need the EmploymentStatus (calculated column) as 'Active' from ReferenceTable, since 'Active' has the maximum of the two available InternalID values (17, 15).
I tried the following code (Calculated Column in SourceTable):
EmploymentStatus_SourceTable_CalculatedColumn =
VAR tabl1 =
SUMMARIZE (
ReferenceTable,
ReferenceTable[EmployeeEmail],
"MaxInteralID", MAX ( ReferenceTable[InternalID] )
)
VAR tabl2 =
FILTER (
ReferenceTable,
ReferenceTable[InternalID] IN VALUES ( tabl1[MaxInteralID] )
)
VAR NewCol =
LOOKUPVALUE (
tabl2[EmploymentStatus],
tabl2[EmployeeEmail], SourceTable[EmployeeEmail]
)
RETURN
NewCol
I realize that I cannot use the column generated from the table variable.
For example, tabl1[MaxInteralID], tabl2[EmployeeStatus], tabl2[EmployeeEmail] - are all invalid.
Any idea on how to handle this? You can even provide me with a solution that does not use variables at all. Am okay with any solution.
Similar to here, you can find the maximal ID for each email and look up the status for that ID.
Table and column names shortened for readability:
CalcCol =
VAR Email = Source[Email]
VAR MaxID = CALCULATE ( MAX ( Ref[ID] ), Ref[Email] = Email )
RETURN
LOOKUPVALUE ( Ref[Status], Ref[Email], Email, Ref[ID], MaxID )

Power BI - Getting the most recent value from a related table

I know this must be extremely simple, but every example I can find online only works within a single table. I've simplified my situation to these two tables:
I want to add a calculated column to the first table, showing the most recent value for that id. It also needs to work with text.
There are a variety of ways to do this kind of thing as I've explained before and all of the solutions there can be adjusted to work in this case.
Doing this as a calculated column and with a second table, you need to make sure you are using row context and filter context appropriately.
Here's are a couple different possibilities I think may work:
MostRecentValue =
MAXX ( TOPN ( 1, RELATEDTABLE ( Table2 ), Table2[date] ), Table2[value] )
In this one, RELATEDTABLE is doing the work of filtering Table2 to only the rows where id matches Table1.
MostRecentValue =
VAR PrevDate = CALCULATE ( MAX ( Table2[date] ) )
RETURN CALCULATE ( MAX ( Table2[value] ), Table2[date] = PrevDate )
The relationship is more subtle here. Wrapping the MAX in CALCULATE forces a context transition so that the row context (which includes id) is applied to Table2 as filter context.

PowerBI - Use Value in Table View as Filter Parameter

This doesn't seem like it should be too complicated, but I'm not quite sure how to get it working.
I have a table in PowerBI with the following columns:
The columns in the database have an entry for Submitter and QAer
The QAs Posted column is basically just a COUNT of the Submitter
For QAs Pulled, I need to get the count of rows where the particular Submitter (in the first column) is listed as the QAer.
Is this something I can do?
Any help is appreciated, thanks!
EDIT: More about the data model - here's a screenshot example.
I think you are looking for something like this:
Measure =
COUNTROWS (
FILTER (
ALL( 'datatabel' ),
'datatabel'[QAer] = SELECTEDVALUE ( 'datatabel'[Submitter] )
)
)

DAX - Stuck on dynamic MAXX column formula

I'm working in Power BI.
I have a table with member card usage data called NonSameDayUses:
https://www.screencast.com/t/yeSjoqonZ
I have another table with member card add data called AddsOnly:
https://www.screencast.com/t/zlPBRWaDqC
The tables are related by the GUID_TranDate2 field. I am trying to add a column to NonSameDayUses that provides the date just before the use date (to calculate when the amount used was added to their card). I have tried a million things, but this is my current formula and I can't figure out what is wrong with it:
DateAdded =
MAXX (
FILTER (
AddsOnly,
AND (
AddsOnly[member_guid] = [member_guid],
AddsOnly[ValueAddDate] < [TransactionDate]
)
),
AddsOnly[TransactionDate]
)
Neither filter is working for me. If I try it with just the first argument (member_guid), I get blanks. If I try with the second (dates) I get the max date for the whole table with no filtering.
Any help would be sooooooooooo appreciated, as I am currently banging my head against the wall! :)
Try qualifying all the column names, it should work:
DateAdded =
MAXX(
FILTER(
AddsOnly
, AND(
AddsOnly[member_guid]
= NonSameDayUses[member_guid]
, AddsOnly[ValueAddDate]
< NonSameDayUses[TransactionDate]
)
)
, AddsOnly[TransactionDate]
)