Hi I'm a beginner in Power bi
I have two tables
Table 1 has Dates, Unique IDs and Emails
Table 2 has list of Unique IDs same as the ones in table 1
Using DAX, I want to add a calculated column to the 2nd table to get the most recent email from table 1 based on the unique id and also avoid Email Values
Thank you.
with DAX measure - if both tables have a relationship
Measure1 =
CALCULATE (
MAX ( 'Table 1'[Email] ),
FILTER (
'Table 1',
'Table 1'[Date]
= CALCULATE (
MAX ( 'Table 1'[Date] ),
ALLEXCEPT ( 'Table 1', 'Table 1'[Unique_ID] )
)
)
)
with DAX calculated column - if both tables have a relationship
Column1 =
CALCULATE (
CALCULATE (
MAX ( 'Table 1'[Email] ),
FILTER (
'Table 1',
'Table 1'[Date]
= CALCULATE (
MAX ( 'Table 1'[Date] ),
ALLEXCEPT ( 'Table 1', 'Table 1'[Unique_ID] )
)
)
)
)
with DAX measure - if both tables have no relationship
Measure2 =
CALCULATE (
MAX ( 'Table 1'[Email] ),
FILTER (
'Table 1',
'Table 1'[Date]
= CALCULATE (
MAX ( 'Table 1'[Date] ),
ALLEXCEPT ( 'Table 1', 'Table 1'[Unique_ID] )
)
),
TREATAS ( VALUES ( 'Table 2'[Unique_ID] ), 'Table 1'[Unique_ID] )
)
with DAX calculated column - if both tables have no relationship
Column2 =
CALCULATE (
CALCULATE (
MAX ( 'Table 1'[Email] ),
FILTER (
'Table 1',
'Table 1'[Date]
= CALCULATE (
MAX ( 'Table 1'[Date] ),
ALLEXCEPT ( 'Table 1', 'Table 1'[Unique_ID] )
)
),
TREATAS ( VALUES ( 'Table 2'[Unique_ID] ), 'Table 1'[Unique_ID] )
)
)
Oh sorry I just realized you're already having a table2. Now I've split the answer in two party. New Column and New Table.
Creating NEW COLUMN
In this case you could try:
Most_Recent_Mail = SELECTCOLUMNS(TOPN(1,FILTER(table1, table1[Unique_ID] = table1[Unique_ID]), table1[Date]), "mail", Tabelle1[mail])
Creating NEW TABLE
I copied your table:
Now we can create a new table:
And use the following DAX code to fill this table:
New_Table = SUMMARIZE(table1,table1[Unique_ID], "Most_Recent_Mail",
CALCULATE(VALUES(table1[mail]), FILTER(all(table1), table1[Date] = max(table1[Date]))))
SUMMARIZE is like a group function.
With CALCULCATE you can create expressions in a filtered context.
With FILTER you can create a new context/subset.
ALL is ignoring all existing filters an returns the full dataset.
Result:
PS: Just to understand. If you only wanted the date, the formula would be much shorter
table2 = SUMMARIZE(table1,table1[Unique_ID], "Max_Date",
max(table1[Date]))
And if you would like to have both, you can add as much columns you want:
New_Table = SUMMARIZE(table1,table1[Unique_ID],
"Max_Date", max(table1[Date]),
"Most_Recent_Mail",
CALCULATE(VALUES(table1[mail]), FILTER(all(table1), table1[Date] = max(table1[Date]))))
Related
I'm conducting an exercise around examining test results after tutoring has occurred. Essentially looking at the rates of "pass" post tutoring within the context of a given student. Where the ultimate outcome would be:
pass rate after tutoring = [count passes]/[count test date] WHERE test date > tutoring date.
For example:
Ideally, the final output of the measure would be = 1 (1/1)
Would anyone be able to point me in the direction of achieving this through a Power BI measure?
I've attempted the following to get the single oc with no luck:
Measure 3 = CALCULATE(COUNT(Table[Test Pass?]),FILTER(Table,Table[Test Date]>CALCULATE(Min(Table[Tutoring Date]),FILTER(Table,Table[Tutor (?)] <> BLANK ))))
Where I would then use the student column in a matrix with the measure to group pass rates post tutoring by student
I have used this simple flat table data model:
You can calculate this with a measure that needs to be evaluated with your Student column:
Pass Rate After Tutoring =
VAR _tutor_date =
CALCULATE (
MAX ( 'Table'[Tutoring Date] ),
ALLEXCEPT ( 'Table', 'Table'[Student] )
)
VAR _tests_post_tutor =
CALCULATE (
COUNTROWS ( 'Table' ),
ALLEXCEPT ( 'Table', 'Table'[Student] ),
'Table'[Test Date] > _tutor_date
)
VAR _successes =
CALCULATE (
COUNTROWS ( 'Table' ),
ALLEXCEPT ( 'Table', 'Table'[Student] ),
'Table'[Test Date] > _tutor_date,
'Table'[Test Pass]
)
RETURN
DIVIDE ( _successes, _tests_post_tutor )
But this assumes that students are only tutored for one specific test, and are tutored once.
I have a table entry for multiple Product and Many different features. All the data has date time in shown format.
As we can see that all products has entries across different time. My aim is to filter the details related to the product by latest time of everyday.
I was able to split the Time col in Date col & Time col separately.
You can achieve that by creating a new table in Dax for eg. (because I don't know what do you want to do with rows with the same timestamp I show both rows for C)
YourFilteredTable =
VAR __trt =
TREATAS (
SELECTCOLUMNS (
ADDCOLUMNS (
SUMMARIZE (
ADDCOLUMNS (
ALL ( YourTable[Product], YourTable[Time] ),
"day", DATEVALUE ( YourTable[Time] )
),
YourTable[Product],
[day]
),
"MaxDate", CALCULATE ( MAX ( YourTable[Time] ) )
),
"Prod", [Product],
"MaxDate", [MaxDate]
),
YourTable[Product],
YourTable[Time]
)
RETURN
SUMMARIZECOLUMNS (
YourTable[Product],
YourTable[Quant],
YourTable[Time],
__trt
)
I'm back with another issue. I have a sales table with transaction details on the products purchased. I also have a table with warehouse inventory information for each product. I'm trying to get the count of products purchased in a Table visualization with columns from the warehouse inventory table.
I tried both of the measures below, but they both return the total Count for each row rather than sliced by product. Any help would be greatly appreciated!
NumProductsfromSales1 = calculate([Count], treatas(values('Sales'[Product]), 'Inventory'[Product]))
NumProductsfromSales2 =
var lineage = treatas(values('Sales'[Product]), 'Inventory'[Product])
var tbl = calculatetable('Inventory Detail', KEEPFILTERS(lineage))
var result = calculate(sumx(tbl, [Count]))
return result
From this source, we see TREATAS works as follows.
[Filtered Measure] :=
CALCULATE (
<target_measure>,
TREATAS (
VALUES ( <lookup_granularity_column> ),
<target_granularity_column>
)
)
is equivalent to
[Filtered Measure] :=
CALCULATE (
<target_measure>,
INTERSECT (
ALL ( <target_granularity_column> ),
VALUES ( <lookup_granularity_column> )
)
)
The important part is the ALL function. That's why you're losing the filter context from the rows in the visual.
I'm not sure if this is the most efficient solution, but I think if you add Inventory as a filter table to your first attempt, it should maintain the filter context on that table from the row in the visual.
NumProductsfromSales1 =
CALCULATE (
[Count],
'Inventory',
TREATAS ( VALUES ( 'Sales'[Product] ), 'Inventory'[Product] )
)
Edit: Regarding your comment, try the following:
a =
VAR top5prod =
SELECTCOLUMNS (
TOPN (
5,
SUMMARIZE ( Sales, Sales[Product], "Count", [Product Count] ),
[Count]
),
"Product", Sales[Product]
)
RETURN
CALCULATE (
[Product Count],
FILTER ( 'Inventory', 'Inventory'[Product] IN top5prod )
)
Using FILTER isn't as efficient as TREATAS but see if it works.
It's very difficult to answer this sort of question without having anything reproducible to work with.
Story:
I have two date columns in my fact table, one is for orderdate and the second is for orderdate/refund/cancelled date.
I created two relationships between the date table and the fact table.
Active: Date > OrderDate
Inactive: Date > OtherDate
I would like to sum the # of refunds per day using the inactive relationship.
What I tried:
Returns =
VAR summary =
SUMMARIZE (
FILTER ( Query1, Query1[kind] = "refund" ),
Query1[orderId],
"returns", MAX ( Query1[amount] )
)
RETURN
CALCULATE (
MAX ( Query1[amount] ),
USERELATIONSHIP ( Query1[OtherDate], DateTable[Date] ),
summary
)
For some reason, it's using the active date column. Any suggestion on how to fix the above formula?
I'm not sure I understand how you are intending to use the summary variable here, but note that USERELATIONSHIP doesn't affect it at all since it's already computed.
You might not need that variable at all. Try this:
Returns =
CALCULATE (
MAX ( Query1[amount] ),
USERELATIONSHIP ( Query1[OtherDate], DateTable[Date] ),
Query1[kind] = "refund"
)
Request: Calculate the number of Purchased Orders past due. Recalculate when filters applied.
Background: Have 3 tables:
Purchase_Orders containing order [Promise Date],
Received_Orders containing [Received Date],
PO_DIM containing unique Purchase Order Numbers.
Relation: **Purchase_Orders -1 PO_DIM 1- Received_Orders
Measure Sudo Code:
1. CountRows
2. Filter for rows where Purchase_Orders[Promised Date] is not Blank()
3. Filter for rows where Received_Orders[Received Date] is Blank()
4. Filter for rows where Purchase_Orders[Promised Date] < Today()
How would you solve?
I would roll the 3 Filter requirements into one FILTER function, using RELATED to test columns from other tables, something like:
Count PO Pastdue =
CALCULATE (
COUNTROWS ( 'PO_DIM' ),
FILTER (
'PO_DIM',
NOT ( ISBLANK ( RELATED ( Purchase_Orders[Promised Date] ) ) )
&& ISBLANK ( RELATED ( Received_Orders[Received Date] ) )
&& RELATED ( Purchase_Orders[Promised Date] ) < TODAY ()
)
)