Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started
Hi All,
I am new to PBI and this is my data model
My goal is to caculate as follows
1. If user select date December 2023 then the code should first find the common store in Fact table and Store list visited in last 3 months from date selected i.e. the should look for date range of December , Nov and Oct in Fact Table and find common store by doing interecept with Store List. the code that i wrote is
Solved! Go to Solution.
Hi @Mkrishna ,
Then you could try EDATE() function. The function returns the date that is the indicated number of months before or after the start date.
Modified:
Sales Amount =
VAR _Reference_Date =
MAX ( 'Date'[Date] )
VAR Customers =
VALUES ( 'Store List'[Store Number] )
VAR PriorCustomers =
CALCULATETABLE (
VALUES ( 'Fact Table'[Store Number] ),
FILTER (
ALL ( 'Date' ),
'Date'[Date] >= EDATE ( _Reference_Date, 3 )
&& 'Date'[Date] <= _Reference_Date
)
)
VAR CommonCustomers =
INTERSECT ( Customers, PriorCustomers )
VAR Sales =
CALCULATE (
SUM ( 'Fact Table'[Sales] ),
FILTER (
ALL ( 'Fact Table' ),
'Fact Table'[Store Number]
IN CommonCustomers
&& 'Fact Table'[Visit Date] >= EDATE ( _Reference_Date, 3 )
&& 'Fact Table'[Visit Date] <= _Reference_Date
)
)
RETURN
Sales
Best Regards,
Stephen Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi @Mkrishna ,
It seems like you are on the right track with your DAX measures, but we need to adjust the logic to ensure that the sales amount reflects all common customers from the intersected store list, regardless of their maximum visit date within the last three months.
For the third part of your goal, where you want to calculate the sales amount, the issue seems to be with the context transition and the way filters are being applied. Here's a revised version of the `Sales Amount` measure that should help:
Sales Amount =
VAR _Reference_Date = MAX('Date'[Date])
VAR _Previous_Dates = DATESINPERIOD(
'Previous Date'[Date],
_Reference_Date,
-3,
MONTH
)
VAR Customers = VALUES('Store List'[Store Number])
VAR PriorCustomers = CALCULATETABLE(
VALUES('Fact Table'[Store Number]),
FILTER(
ALL('Date'),
'Date'[Date] >= _Reference_Date - 90 && // Adjust to include the last 90 days
'Date'[Date] <= _Reference_Date
)
)
VAR CommonCustomers = INTERSECT(Customers, PriorCustomers)
VAR Sales = CALCULATE(
SUM('Fact Table'[Sales]),
FILTER(
ALL('Fact Table'),
'Fact Table'[Store Number] IN CommonCustomers &&
'Fact Table'[Visit Date] >= _Reference_Date - 90 &&
'Fact Table'[Visit Date] <= _Reference_Date
)
)
RETURN Sales
In this revised measure:
Please replace the placeholder 90 with the actual number of days that correspond to three months in your context, as months can have varying numbers of days.
For the Total Sales in 3 months measure, ensure that you are not filtering out the stores with visits in October and November. Here's a simplified version that should work:
Total Sales in 3 months =
VAR _Reference_Date = MAX('Date'[Date])
VAR _Start_Date = EDATE(_Reference_Date, -3)
RETURN
IF(
ISEMPTY('Fact Table'),
BLANK(),
CALCULATE(
SUM('Fact Table'[Sales]),
FILTER(
ALL('Fact Table'),
'Fact Table'[Visit Date] >= _Start_Date &&
'Fact Table'[Visit Date] <= _Reference_Date &&
'Fact Table'[Store Number] IN VALUES('Store List'[Store Number])
)
)
)
This measure calculates the total sales for the last three months, including stores with a maximum visit date in October or November, not just December.
Please try these revised measures and let me know if they work for you. If you encounter any further issues, please feel free to as me.
Best Regards,
Stephen Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Thank you @v-stephen-msft for the reply. I report is montlhy one. I cannot do -90 days as some month will have 31 and some 30 and Feb can have 29/28 days.
Hi @Mkrishna ,
Then you could try EDATE() function. The function returns the date that is the indicated number of months before or after the start date.
Modified:
Sales Amount =
VAR _Reference_Date =
MAX ( 'Date'[Date] )
VAR Customers =
VALUES ( 'Store List'[Store Number] )
VAR PriorCustomers =
CALCULATETABLE (
VALUES ( 'Fact Table'[Store Number] ),
FILTER (
ALL ( 'Date' ),
'Date'[Date] >= EDATE ( _Reference_Date, 3 )
&& 'Date'[Date] <= _Reference_Date
)
)
VAR CommonCustomers =
INTERSECT ( Customers, PriorCustomers )
VAR Sales =
CALCULATE (
SUM ( 'Fact Table'[Sales] ),
FILTER (
ALL ( 'Fact Table' ),
'Fact Table'[Store Number]
IN CommonCustomers
&& 'Fact Table'[Visit Date] >= EDATE ( _Reference_Date, 3 )
&& 'Fact Table'[Visit Date] <= _Reference_Date
)
)
RETURN
Sales
Best Regards,
Stephen Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Check out the September 2024 Power BI update to learn about new features.
Learn from experts, get hands-on experience, and win awesome prizes.
User | Count |
---|---|
111 | |
102 | |
98 | |
38 | |
37 |
User | Count |
---|---|
152 | |
125 | |
75 | |
74 | |
63 |