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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

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
Anonymous
Not applicable

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

Thanks @Anonymous , 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?

Anonymous
Not applicable

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

HI @Anonymous , 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
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.