The ultimate Fabric, Power BI, SQL, and AI community-led learning event. Save €200 with code FABCOMM.
Get registeredCompete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.
I have a date table whereby I set the date range to the end of year as is recommended. But when I need to use that field to filter a measure (as is recommended instead of filtering with a column from a fact table) I need to get the value that is max date in the fact table as the max date from the date table will return nothing in the fact table.
e.g.
Sales table date range 01/01/2023 - 05/04/2023
Date table date range 01/01/2023 - 31/12/2023
------------------------------------------
measure:
Sales Last Month =
CALCULATE(
SUM('Sales'[Profit]),
'Date'[Date] = MAX('Date'[Date]) - 1,
ALLSELECTED('Date')
)
-------------------------------------------
The max date will return null as there is no max date of 31/12/2023 in the sales table what is a better way to rewrite this measure. keeping in view that the filter column should be from a dimension table not a fact table as is recommended.
Solved! Go to Solution.
Try this instead:
Try this instead:
@akhaliq7 A couple potential problems with this. First, subtracting 1 from a date does not give you last month, it just gives you the previous day. Are you trying to calculate the sales for the last month in the fact table that is within the range of your slicer? If that is the case then maybe something like this:
Measure =
VAR __MaxDate = MAX('Sales'[Date])
VAR __YearMonth = YEAR(__MaxDate) * 100 + MONTH(__MaxDate)
VAR __Table =
SUMMARIZE(
FILTER(ALLSELECTED('Sales'), YEAR([Date]) * 100 + MONTH([Date]) = __YearMonth),
[Date],
"__Profit", SUM('Sales'[Profit])
)
VAR __Result = SUMX( __Table, [__Profit])
RETURN
__Result
User | Count |
---|---|
26 | |
10 | |
8 | |
6 | |
5 |
User | Count |
---|---|
33 | |
13 | |
12 | |
9 | |
7 |