March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early bird discount ends December 31.
Register NowBe one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now
Hi,
I have two distinct questions relation to solving the following matters. One is pure DAX related, and the other is why the grand totals in my pivot table does not compute accurately. I do hower think they are related in some way.
Basic question: I wish to calculate the total number of "active machines" in a certain year to perform a cost analysis. This means each machine is active between the time of Purchasing and Decomission.
The following INVENTORY table shows this:
Machine nr | PurchaseDate | Decomissioned |
1 | 2017-01-02 | 2018-05-10 |
2 | 2018-01-02 | 2021-09-13 |
3 | 2010-06-05 | 2016-09-13 |
4 | 2018-01-02 | 2021-09-13 |
5 | 2018-01-02 | 2021-09-13 |
The Dax formula im trying to write should include the active machine to the year it was decomissioned.
for example: the number of machines active during 2018 would be 4 (Machine number 1, 2, 4, 5) even though machinie 1 was decommisioned during 2018.
My Dax formula looks like this (using a date table) but it does not seem to work correctly:
(for context the AO__Tabell is registering costs relating to the machines each year which we sum for each corresponding year. )
=VAR maxDate=ENDOFYEAR(AO___Tabell[Reg. date]) -- Filtered by context in the pivot table.
RETURN CALCULATE(COUNTROWS(INVENTORY);FILTER(INVENTORY;INVENTORY[PurchaseDate] <=maxDate && (INVENTORY[Decomissioned] >maxDate || INVENTORY[Decommisioned] IN DATESINPERIOD('Date'[Date];STARTOFYEAR('Date'[Date]);1;YEAR))))
In addition, while this DAX does not work perfectly - I get very wrong Grand totals in my pivot table (see picture below). Im guessing this has to do with the grand totals in pivot tables not taking into account the DAX filters im using. If that is the case, how can the DAX formula be changed to accomodate this?
Very thankful for any help that is available!
Sincerely,
Gustaf
Solved! Go to Solution.
Hi @gustafwallin ,
According to your description, I made a sample, and here is my solution.
Create a DATE table.
DATE =
CALENDAR ( MIN ( 'Table'[PurchaseDate] ), MAX ( 'Table'[Decomissioned] ) )
Do not establish a relationship between two tables.
Create a measure to display the active machine nr.
active machines =
VAR _slice =
ENDOFYEAR ( 'DATE'[Date] )
RETURN
IF (
COUNTROWS (
FILTER (
_slice,
MAX ( 'Table'[PurchaseDate] ) <= _slice
&& MAX ( 'Table'[Decomissioned] ) > _slice
|| MAX ( 'Table'[Decomissioned] )
IN DATESINPERIOD ( 'DATE'[Date], STARTOFYEAR ( 'DATE'[Date] ), 1, YEAR )
)
) > 0,
MAX ( 'Table'[Machine nr] ),
BLANK ()
)
Create a measure to calculate the total numbers of the active machines.
number of active machines =
VAR _slice =
ENDOFYEAR ( 'DATE'[Date] )
RETURN
CALCULATE (
COUNTROWS ( 'Table' ),
FILTER (
'Table',
'Table'[PurchaseDate] <= _slice
&& (
'Table'[Decomissioned] > _slice
|| 'Table'[Decomissioned]
IN DATESINPERIOD ( 'DATE'[Date], STARTOFYEAR ( 'DATE'[Date] ), 1, YEAR )
)
)
)
Put “Year” of DATE into slicer, and then select the year, you will get the Final output:
I attach my sample below for your reference.
Best Regards,
Community Support Team _ xiaosun
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Hi @gustafwallin ,
According to your description, I made a sample, and here is my solution.
Create a DATE table.
DATE =
CALENDAR ( MIN ( 'Table'[PurchaseDate] ), MAX ( 'Table'[Decomissioned] ) )
Do not establish a relationship between two tables.
Create a measure to display the active machine nr.
active machines =
VAR _slice =
ENDOFYEAR ( 'DATE'[Date] )
RETURN
IF (
COUNTROWS (
FILTER (
_slice,
MAX ( 'Table'[PurchaseDate] ) <= _slice
&& MAX ( 'Table'[Decomissioned] ) > _slice
|| MAX ( 'Table'[Decomissioned] )
IN DATESINPERIOD ( 'DATE'[Date], STARTOFYEAR ( 'DATE'[Date] ), 1, YEAR )
)
) > 0,
MAX ( 'Table'[Machine nr] ),
BLANK ()
)
Create a measure to calculate the total numbers of the active machines.
number of active machines =
VAR _slice =
ENDOFYEAR ( 'DATE'[Date] )
RETURN
CALCULATE (
COUNTROWS ( 'Table' ),
FILTER (
'Table',
'Table'[PurchaseDate] <= _slice
&& (
'Table'[Decomissioned] > _slice
|| 'Table'[Decomissioned]
IN DATESINPERIOD ( 'DATE'[Date], STARTOFYEAR ( 'DATE'[Date] ), 1, YEAR )
)
)
)
Put “Year” of DATE into slicer, and then select the year, you will get the Final output:
I attach my sample below for your reference.
Best Regards,
Community Support Team _ xiaosun
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Thank you very much for this elaborate reply. This was way more than I hoped for. This definately does the trick, even though I'll have to sharpen my DAX skills to fully understand the formulas. I'll try and make the connections in Power BI to my larger Data model!
Again, thanks a lot!
@gustafwallin , Refer if these two can help
Hi Amit!
Thanks for the links, that is some clean looking variants that seem to somewhat apply to my specific problem.
I'll have to look into it more closely, but the first example you sent seemed very similar.
Again, Thank you!
March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!
Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.
Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.
User | Count |
---|---|
23 | |
15 | |
12 | |
9 | |
8 |
User | Count |
---|---|
41 | |
32 | |
29 | |
12 | |
12 |