Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be 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

Reply
gustafwallin
New Member

Dax Cacluate expression with Excel pivot table Grand totals

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 nrPurchaseDateDecomissioned
12017-01-022018-05-10
22018-01-022021-09-13
32010-06-052016-09-13
42018-01-022021-09-13
52018-01-022021-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?  

 

gustafwallin_0-1665068452352.png

 

Very thankful for any help that is available! 

 

Sincerely, 

Gustaf

 

 

1 ACCEPTED SOLUTION
v-xiaosun-msft
Community Support
Community Support

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.

vxiaosunmsft_0-1665135132983.png

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:

vxiaosunmsft_1-1665135177067.png

 

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.

View solution in original post

4 REPLIES 4
v-xiaosun-msft
Community Support
Community Support

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.

vxiaosunmsft_0-1665135132983.png

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:

vxiaosunmsft_1-1665135177067.png

 

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! 

amitchandak
Super User
Super User

@gustafwallin , Refer if these two can help

https://community.powerbi.com/t5/Community-Blog/HR-Analytics-Active-Employee-Hire-and-Termination-tr...


https://community.powerbi.com/t5/Community-Blog/How-to-divide-distribute-values-between-start-date-o...

 

 

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

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! 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.