I am hopeful you will be able to help me or point me in the right direction to work out a DAX formula to return a calculated table.
I have spend hours trying to figure it out but but have hit a wall and cannot move further.
For the purpose of this illustration, I have a simple table which contains orders. Customer can purchase an order from two different shops. I am trying to work out which are the order numbers of red products which have been purchased before the max expiry date of a yellow product, if sold to the same customer at the same shop.
My table is as follows:
Order ID Office Customer Order Date Expiry Date Product
1 Shop1 Cust1 02/02/2022 27/08/2022 Red
2 Shop1 Cust1 15/06/2021 04/02/2022 Red
3 Shop1 Cust1 30/09/2022 29/04/2023 Blue
4 Shop1 Cust1 07/05/2021 18/12/2021 Yellow
5 Shop1 Cust2 30/05/2021 23/05/2022 Red
6 Shop2 Cust2 08/02/2022 13/01/2023 Yellow
7 Shop1 Cust2 03/09/2022 13/04/2023 Blue
8 Shop1 Cust3 24/04/2021 11/07/2021 Yellow
9 Shop1 Cust3 23/02/2022 21/01/2023 Yellow
10 Shop1 Cust3 03/06/2022 24/11/2022 Blue
11 Shop1 Cust3 04/09/2021 28/08/2022 Red
12 Shop1 Cust3 05/09/2021 28/08/2022 Red
The desired output of the calculated table is as follows:
Order ID
2
11
12
As explained, I need to retrieve all the order IDs for the red product purchased by the same customer who has also purchased a yellow product at the same shop as the red product and where the max expiry date of the yellow product is after the order date of the red product.
My table has the following Red products and here is the explanation why they should/shouldn't be included:
Order ID 1 - Don't flag this as max expiry date of Yellow product (order #4 - 18/12/21) is before the order date of Red product (order #1 - 02/02/22)
Order ID 2 - Flag this as max expiry date of the Yellow product (order #4 - 18/12/21) is after the order date of Red product (order #2 - 15/06/21)
Order ID 5 - Don't flag it as the Yellow product (order #6) was sold in a different Shop as Red product (there is no max expiry date of Yellow product in the same shop as the Red product order #5)
Order ID 11 - Flag this as the max expiry date of Yellow product (order #9) is after the order date of Red product (order #11)
Order ID 12 - Flag this as the max expiry date of Yellow product (order #9) is after the order date of Red product (order #12)
Hope the above example is clear.
Your advice how to achieve this will be greatly appreciated.
One of possible solution:
Table =
var redproducts = CALCULATETABLE(Orders, Orders[Product] ="Red")
var yellowproducts = SELECTCOLUMNS(CALCULATETABLE(Orders, Orders[Product] = "Yellow"), "YellowCust", [Customer], "YellowStore", [Office], "YellowExpiration", [Expiry Date])
return GENERATE(redproducts, FILTER(yellowproducts, [Customer] = [YellowCust] && [Office] = [YellowStore] && [Order Date] < [YellowExpiration]))
of course, you can select only ID at the end;
Alternative:
=
FILTER(
ADDCOLUMNS(
'Table',
"Yellow Count",
VAR Office = 'Table'[Office]
VAR Customer = 'Table'[Customer]
RETURN
CALCULATE(
MAX( 'Table'[Expiry Date] ),
FILTER(
'Table',
'Table'[Office] = Office
&& 'Table'[Customer] = Customer
&& 'Table'[Product] = "Yellow"
)
)
),
'Table'[Product] = "Red"
&& [Yellow Count] > 'Table'[Order Date]
)
Related
I am working to get cumulative distinct count of uids on daily basis. My dataset consists dates and UserIDs active on that date. Example : Say there are 2 uids (235,2354) appeared on date 2022-01-01 and they also appeared on next day with new uid 125 (235,2354,125) on 2022-01-02 At this point i want store cumulative count to be 3 not 5 as (user id 235 and 2354 already appeared on past day ).
My Sample Data looks like as follows:
https://github.com/manish-tripathi/Datasets/blob/main/Sample%20Data.xlsx
enter image description here
and my output should look as follows:
enter image description here
Here's one way that seems to work, using your linked Excel sheet as the data source.
Create a new table:
Table 2 = DISTINCT('Table'[Date])
Add the columns:
MAU = CALCULATE(
DISTINCTCOUNT('Table'[User ID]),
'Table'[Date] <= EARLIER('Table 2'[Date]))
DAU = CALCULATE(DISTINCTCOUNT('Table'[User ID]),
'Table'[Date] = EARLIER('Table 2'[Date]))
Result from your Excel data
I have two tables in Power BI as follows:
COUNTRIES
COD COUNTRY
1 BRAZIL
2 ARGENTINA
3 CHILE
4 BRASIL
5 COLOMBIA
6 ARGENTINA
7 URUGUAI
SALES
COD DATE
1 2021-01-02
2 2021-10-01
3 2019-09-04
1 2018-07-05
7 2019-04-10
There's a relationship between the two tables, on the COD column.
I need to count how many countries (column "COUNTRY" from the table "COUNTRIES") have a status CHURN. It's considered CHURN when their latest order from the table "SALES" is more than 180 days, using today as a reference.
I know that I need to group by the MAX date per country, do a DATEDIFF, and then do a COUNT. I've tried using ALL and SUMMARIZE, but I haven't found a way to solve this problem.
Are you able to add a calculated column to store the max sales date for each country in your COUNTRIES table? Either in Power BI or directly in your database. If so, here's one solution with 2 steps.
Create a MaxSalesDate column in your COUNTRIES table. DAX for a calculated column below:
MaxSalesDate =
VAR COD = COUNTRIES[COD]
RETURN MAXX(FILTER(SALES, SALES[COD] = COD), SALES[DATE])
Create a measure that counts the number of MaxSalesDate values that are older than 180 days old:
CountCHURN = COUNTX(COUNTRIES, IF(DATEDIFF(COUNTRIES[MaxSalesDate], TODAY(), Day) > 180, 1))
I have a DAX measure in my Power BI dashboard that is:
Avg Rating =
IF(
SUM(data[Review Count]) = 0,
BLANK(),
SUM(data[Total Stars])/SUM(data[Review Count])
)
Total Stars and Review Count are both calculated columns:
Table with Total Stars and Review Count
My table has for product A:
January - 10 reviews, all 5 stars --> (AVG: 5 stars for January)
February - 10
reviews, all 3 stars --> (AVG: 3 stars for February)
My PBI visual is coming up with the following for Product A:
January - (AVG: 4 stars)
February - (AVG: 4 stars)
Should I be using a different DAX measure to ensure that the averages can be filtered by product, month, etc.?
I've been struggling with this and hope someone is able to help...
I have a table with sales for products over multiple years. There is a measure that gives me the total revenue for each year by customer, ignoring product sold:
totalRevenueMeasure =
CALCULATE (
SUM ( test[Revenue] ),
ALLEXCEPT ( test, test[company], test[year] )
)
Year company Product revenue totalRevenueMeasure rankx (revenue in year)
2018 company a shoes 100 300 1
2018 company a mugs 200 300 1
2018 Company b shoes 250 250 2
2019 company a lamps 300 300 2
2019 Company b shoes 350 450 1
2019 Company b mugs 100 450 1
2019 Company c mugs 100 100 3
2020 company a shoes 150 150 2
2020 Company c lamps 200 200 1
The closest I got to the RANKX measure is below but this doesn't give the correct results. The expected output is in the RANKX column of the table above.
Customer Rank =
RANKX(
ALLSELECTED( test[company],test[year]),
[TotalRevenueMeasure],
,
DESC,
Dense
)
Thanks in advance for pointers, DAX is still eluding me a bit and there might be a better way to go about it.
Following the recommendation from Alexis, success with test data but live skips some rows in rank - year 2019 doesn't have a rank #1 but has 2 rank #2. I guess this must be some kind of data issue...
You're very close. The problem is that you are looking to rank each year separately but you've removed the Year filter context with your ALLSELECTED function.
Take out the second argument in ALLSELECTED so that you only have company (since you don't actually want to rank over all years for each row).
I want to create a visualisation from a dataset which shows percentages that change accordingly when filters are used.
I have a dataset like the below but with over 1 million rows of data covering 18 months. All the fields are text except Month which is a date and SUMofAPPTS which is numerical.
SUPP GEOG1 MODE STATUS TYPE TIME Month Day SUMofAPPTS
AA 00D Face Att 1 1 Day 2018-06 Sun 12
AA 00D Face Att 1 1 Day 2018-06 Mon 119
AA 00D Face Att 1 4 Unk 2018-06 Tues 98
BB 00D Tel DNA 2 1 Day 2018-06 Weds 98
BB 00D Online DNA 3 1 Day 2018-06 Thurs 126
CC 00D Face DNA 1 2 Day 2018-07 Sun 8
I would like a measure which calculates the percentage of SUMofAPPTS by Day and MODE (and the same but for STATUS, TYPE and TIME) which changes when filters are placed on the other fields.
So I think I need to make this simple calculation (which would work in a column if I just wanted to know the percentage per row of the whole dataset) more dynamic so that it works when I filter the data:
PERCENT = 'dataset'[SUMofAPPTS]/SUM('dataset'[SUMofAPPTS])
The end result will be a stacked bar chart with the following attributes:
Day as the Axis
PERCENT as the Value
MODE, STATUS, TYPE or TIME as the Legend
Ability to filter by one, many or all of the fields except Day and SUMofAPPTS
First, create a measure for aggregating SUMofAPPTS:
Total APPTS = SUM(Data[SUMofAPPTS])
Second, create a measure for the percentage:
APPTS % of Selected
= DIVIDE(
[Total APPTS],
CALCULATE([Total APPTS], ALLSELECTED()))
This measure recalculates [Total APPTS] ignoring all filters except those selected by a user (on slicers etc).
Result:
after selections:
Edit
If you need to breakdown by Day (or any other field), you can re-introduce filters like this:
APPTS % of Selected
= DIVIDE(
[Total APPTS],
CALCULATE([Total APPTS], ALLSELECTED(), VALUES(Data[Day])))