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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
G_Whit-UK
Helper II
Helper II

Calculate average age per category

Hi,

 

I'm looking for a DAX / PBI Desktop based solution to calculate the average age of the items within a category.  The data set is such that we get a new transaction file every day, and the same transactions can appear on sucessive daily reports.  Therefore the age for each item being used by the calculation needs to be the maximum for that item, then the average for all the items within that category.  To make things more complicated, the report has a date slider, so the calculation needs to respect the selected date ranges.

 

Example data: Daily files appended into a single table:

DateTrade RefCategoryColumn XColumn YAge
01/06/20231Axy0
01/06/20232Axy0
01/06/20233Bxy0
01/06/20234Bxy0
01/06/20235Axy0
02/06/20231Axy1
02/06/20232Axy1
02/06/20235Axy1
02/06/20236Bxy0
02/06/20237Bxy0
03/06/20232Axy2
03/06/20236Bxy1
03/06/20238Bxy

0

(Column X & Y simply denote that the table has a number of other columns)

 

In my mind, the logic needs to be able to summarise the above table to list the distinct trade refs within the selected date range, then determine the maximum age of that transaction within the selected date range.  Once this temporary data has been derived, we can then calculate the overall average within each category using the maximum ages already calculated.

 

The tempory table I have in mind would look something like this:

CategoryTrade RefAge
A11
A22
A51
B30
B40
B61
B70
B80

 

The output of the above would be as follows:

Distinct Count: A3
Distinct Count: B5
Avg Age: A1.33
Avg Age: B

0.2

 

I've tried things like Summarize, SummarizeColumns (& some others), but I've not quite managed to get this to work.  Does anyone have any good ideas?  Thanks.

4 REPLIES 4
v-shex-msft
Community Support
Community Support

Hi @G_Whit-UK,

It seems like a common two-level aggregate calculation in Dax, you can try to use the following measure formula to get the average of age values based on current category group and max date value.

formula =
VAR summary =
    ADDCOLUMNS (
        SUMMARIZE (
            ALLSELECTED ( Table1 ),
            Table1[Category],
            "LDate", MAX ( Table1[Date] )
        ),
        "Age",
            LOOKUPVALUE (
                Table1[Age],
                Table1[Catetgory], [Catetgory],
                Table1[Date], [LDate]
            )
    )
RETURN
    AVERAGEX ( summary, [Age] )

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

Thanks @v-shex-msft , I think your suggestion is getting me in the correct direction, but it's still not quite there.

 

This is the current measure based on your earlier help:

Test Max Age = 
VAR summary =
    ADDCOLUMNS (
        SUMMARIZE (
            ALLSELECTED ('Global Failing Sales Data'),
            'Global Failing Sales Data'[Asset Type], 
            'Global Failing Sales Data'[Trade Ref], 
            'Global Failing Sales Data'[Region],
            "LDate", MAX ( 'Global Failing Sales Data'[Date] )
        ),
        "Age",
            LOOKUPVALUE (
                'Global Failing Sales Data'[Age],
                'Global Failing Sales Data'[Asset Type], [Asset Type],
                'Global Failing Sales Data'[Trade Ref], [Trade Ref],
                'Global Failing Sales Data'[Region], [Region],
                'Global Failing Sales Data'[Date], [LDate]
            )
    )
RETURN
    AVERAGEX ( summary, [Age] )

 

We are close in that if I select an individual record, the measure returns the correct value - example:

G_WhitUK_2-1688387304445.png

 

 

However, when looking at the ouput using a Matrix visualisation, each region  and asset type has the same value - which isn't correct (it is currently reflecting the overall avg reglardless of asset type and region):

G_WhitUK_1-1688387144916.png

 

What am I missing in the measure formula?  I appreciate your help.  Is it simply the case of somehow amending the "summary" variable?

Hi @G_Whit-UK,

Can you please share a pbix or some dummy data that keep the raw data structure with expected results? It should help us clarify your scenario and test to coding formula.

How to Get Your Question Answered Quickly  

Regards,

Xiaoxin Sheng

Community Support Team _ Xiaoxin
If this post helps, please consider accept as solution to help other members find it more quickly.

HI @v-shex-msft , I've attempted to respond to your last message twice now with details, but for some reason the responce is not being saved by the forum engine.

 

This is a brief version:  I am unable to supply data as I cannot attach files, and it is against company policy to use online drives (like Google Drive).  I'll see if I can give a more detail next week when I have time (3rd time lucky perhaps)...

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! Prices go up Feb. 11th.

Feb2025 Sticker Challenge

Join our Community Sticker Challenge 2025

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

Find out what's new and trending in the Fabric community.