DAX - Stuck on dynamic MAXX column formula - powerbi

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]
)

Related

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.

DAX - Trying to write a measure which needs a relationship that doesnt exist

I need to calculate totals using a relationship which doesnt exist. I have the following anonymised tables:
Team
Eng
Job
Hours
My relationships are:
I want to calculate the total hours where Job.EngID = Hours.EngID AND Job.JobID = Hours.JobID, per team. What I want is:
I am part of the way there, but only if I want to show hours by engineer, not by team
HrsMeasure =
CALCULATE (
SUM ( Hours[Hrs] ),
FILTER ( Hours, Hours[EngID] = MAX ( Job[EngID] ) )
)
This gives me:
Is there anything I can do, without changing the data model/relationships?
See below the PBIX file:
https://1drv.ms/u/s!AuiIgc_S9J5JhbgBkRFKyNPYNoxxNA?e=gZBhi2
Cheers for all help
Edit 1 - So I have tried using an inactive relationship between Jobs and Hours but I still get the wrong values. This is done using a concatenated column of JobID/EngID on both Jobs and Hours tables:
HrsMeasureUSERELATIONSHIPJobEng =
CALCULATE (
SUM ( Hours[Hrs] ),
USERELATIONSHIP(Hours[JobEng],Job[JobEng])
)
Hope someone can help me on this as its driving me bonkers!
Cheers
So the answer was to create concatenated columns for JobID/EngID on both the Hours and Jobs tables. Join these two columns using an inactive relationship, and then use a CALCULATE with a USERELATIONSHIP to activate the inactive relationship - this has done the trick:
HrsMeasureUSERELATIONSHIPJobEng =
CALCULATE (
SUM ( Hours[Hrs] ),
USERELATIONSHIP(Hours[JobEng],Job[JobEng])
)

PowerBI DAX - Identifying first instance based on multiple criteria

Using DAX to identify first instance of a record
I'm faced with trying to identify the first instance in a database where someone (identified by the ID column) has purchased a product for the first time. It's possible for said person to purchase the product multiple times on different days, or purchase different products on the same day. I drummed up an excel formula that gets me there, but am having trouble translating into DAX.
=COUNTIFS(ID,ID,PurchaseDate,"<="&PurchaseDate,Product,Product)
Which results in the correct values in the "First Instance?" Column.
Ideally I won't have to hardcode values, as I would like to use the "Product" column as a parameter in the future. If there are other suggests aside from translating this in DAX, that would also be appreciated! (IE using filters, or other tools in PowerBI)
Thanks in advance!
This is very similar to an answer I gave to another question (which you can find here).
In that question, the request was to see a running count of rows for the given row's criteria (product, year, etc.). We can modify that slightly to get it to work in your problem.
This is the formula I provided in the answer I linked above. The basic concept is to use the EARLIER functions to get the value from the row and pass it into the filter statement.
Running Count =
COUNTROWS(
FILTER(
'Data',
[ProductName] = EARLIER([ProductName]) &&
[Customer] = EARLIER([Customer]) &&
[Seller] = EARLIER([Seller]) &&
[Year] <= EARLIER([Year])
)
)
What I would suggest for your problem is to create this as a TRUE/FALSE flag by simply checking if the running count is 1. This formula will evaluate to a Boolean flag.
First Instance =
COUNTROWS(
FILTER(
'Data',
[ID] = EARLIER([ID]) &&
[Product] = EARLIER([Product]) &&
[Purchase Date] <= EARLIER([Purchase Date])
)
) = 1

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] )
)
)

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

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.