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
Lygral63
Helper I
Helper I

Calculating replacement sales in a measure?

I need help to set up a measure that will calculate future replacement sales based on sales history:

 

A certain product has been sold in the volume illustrated in the sales table: Sales. In the following years the products will be replaced according to the distribution in the table: ReplDist. I need to calculate the replacement volume in any years like illustrated in the lower table.

 

Is it possible to create a measure that will calculate the total replacement in any given year? The example is a bit simple, but explains what I am trying to do. I would like to avoid using calculated columns as I will have to work with data overing more than 100 years of sales and would like to avoid hundreds of columns.

 

Thanks in advance.

 

Sales  ReplDist    
YearSales After x Years% Replaced   
20161200 15%   
20171300 220%   
20181600 350%   
20191500 420%   
20201700 55%   
        
YearSalesReplacementReplacementReplacementReplacementReplacementTotal Replacement
2016120060    60
2017130024065   305
2018160060026080  940
2019150024065032075 1285
2020170060260800300851505
2021xxxxxxx65320750340xxxx
2022xxxxxxxxxx80300850xxxx
2023xxxxxxxxxxxxx75340xxxx
2024xxxxxxxxxxxxxxxx85xxxx
2025xxxxxxxxxxxxxxxxxxxxxxx

 

1 ACCEPTED SOLUTION
Greg_Deckler
Community Champion
Community Champion

@Lygral63 - OK, one last version, fixes both row and column totals. See below PBIX, the final measure includes the first measure definition and then this one:

ReplacementTotals = 
    VAR __ColumnTotal = 
        ADDCOLUMNS(
            '1_Years',
            "__Replacement",[Replacement]
        )
    VAR __RowTotals =
        ADDCOLUMNS(
            '1_Years1',
            "__Replacement",[Replacement]
        )
RETURN
    SWITCH(TRUE(),
        HASONEVALUE('1_Years'[Value]) && HASONEVALUE('1_Years1'[Value]),[Replacement],
        HASONEVALUE('1_Years1'[Value]),SUMX(__ColumnTotal,[__Replacement]),
        SUMX(__RowTotals,[__Replacement])
    )


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

10 REPLIES 10
Lygral63
Helper I
Helper I

@Greg_Deckler  It seems that the measures work fine if I use the full sales history (which covers a number of countries), but if I add a filter to only look a the sales history for specific countries, it seems that the measure is still calculating on the basis of the full sales history (ie. disregarding filters). Is that correct and what should I do to modify the replacement measure to allow filtering of eg. sales history? 

 
Replacement =
VAR __MinYear = MIN('Year (Horizontal)'[Value])
VAR __Sales = MAXX(FILTER(ALL(SalesHistory),[Year]=__MinYear),[Sales])
VAR __StartYear = MIN('Year (Vertical)'[Value])
VAR __Diff = __StartYear - __MinYear + 1
VAR __Percent = MAXX(FILTER('X Years',[X_Years] =__Diff),[Repl_Dist_Annual])
RETURN
__Sales * __Percent

ReplacementTotals =
VAR __ColumnTotal =
ADDCOLUMNS(
'Year (Vertical)',
"__Replacement",[Replacement]
)
VAR __RowTotals =
ADDCOLUMNS(
'Year (Horizontal)',
"__Replacement",[Replacement]
)
RETURN
SWITCH(TRUE(),
HASONEVALUE('Year (Vertical)'[Value]) && HASONEVALUE('Year (Horizontal)'[Value]),[Replacement],
HASONEVALUE('Year (Horizontal)'[Value]),SUMX(__ColumnTotal,[__Replacement]),
SUMX(__RowTotals,[__Replacement])


Lygral63
Helper I
Helper I

@Greg_Deckler All clear. Thanks.

Greg_Deckler
Community Champion
Community Champion

@Lygral63 Not really, the first measure returns the correct value at the line level. The second measure uses the first measure. You use the second measure in your visual. The first measure is there used behind-the-scenes.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Greg_Deckler
Community Champion
Community Champion

@Lygral63 - OK, one last version, fixes both row and column totals. See below PBIX, the final measure includes the first measure definition and then this one:

ReplacementTotals = 
    VAR __ColumnTotal = 
        ADDCOLUMNS(
            '1_Years',
            "__Replacement",[Replacement]
        )
    VAR __RowTotals =
        ADDCOLUMNS(
            '1_Years1',
            "__Replacement",[Replacement]
        )
RETURN
    SWITCH(TRUE(),
        HASONEVALUE('1_Years'[Value]) && HASONEVALUE('1_Years1'[Value]),[Replacement],
        HASONEVALUE('1_Years1'[Value]),SUMX(__ColumnTotal,[__Replacement]),
        SUMX(__RowTotals,[__Replacement])
    )


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler  Thanks again. This has been such a great help. I have been struggling with this for some time, so looking forward to test the measure and the principle with the real data. 🙂

@Greg_Deckler  If I understand you correctly, I will now need to combine the two measures (see below). If it is not asking too much, could you show me how you would make the correct combination? Any help highly appreciated ...

Replacement =
VAR __MinYear = MIN('1_Years1'[Value])
VAR __Sales = MAXX(FILTER(ALL('1_Sales'),[Year]=__MinYear),[Sales])
VAR __CurrentYear = MIN('1_Years'[Value])
VAR __Diff = __CurrentYear - __MinYear + 1
VAR __Percent = MAXX(FILTER('1_ReplDist',[After x Years]=__Diff),[% Replaced])
RETURN
__Sales * __Percent

 


ReplacementTotals =
VAR __ColumnTotal =
ADDCOLUMNS(
'1_Years',
"__Replacement",[Replacement]
)
VAR __RowTotals =
ADDCOLUMNS(
'1_Years1',
"__Replacement",[Replacement]
)
RETURN
SWITCH(TRUE(),
HASONEVALUE('1_Years'[Value]) && HASONEVALUE('1_Years1'[Value]),[Replacement],
HASONEVALUE('1_Years1'[Value]),SUMX(__ColumnTotal,[__Replacement]),
SUMX(__RowTotals,[__Replacement])
)

Greg_Deckler
Community Champion
Community Champion

@Lygral63 - How about this? See updated PBIX below. Totals need some work.

Replacement = 
    VAR __MinYear = MIN('1_Years1'[Value])
    VAR __Sales = MAXX(FILTER(ALL('1_Sales'),[Year]=__MinYear),[Sales])
    VAR __CurrentYear = MIN('1_Years'[Value])
    VAR __Diff = __CurrentYear - __MinYear + 1
    VAR __Percent = MAXX(FILTER('1_ReplDist',[After x Years]=__Diff),[% Replaced])
RETURN
    __Sales * __Percent

For Totals, see my post about it here: https://community.powerbi.com/t5/DAX-Commands-and-Tips/Dealing-with-Measure-Totals/td-p/63376

Also, this Quick Measure, Measure Totals, The Final Word should get you what you need:
https://community.powerbi.com/t5/Quick-Measures-Gallery/Measure-Totals-The-Final-Word/m-p/547907

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler  This is just great. It will save me from creating hundreds of measures. Not entirely sure what is going on with the totals, but as you say, a bit of work is still needed. Once the totals are working, I will definitely try the measure (probably with a few adjustments) on the real dataset. Hope it works.

 

Thanks again.

Greg_Deckler
Community Champion
Community Champion

@Lygral63 Might be other ways of doing this, like having a single measure and having a matrix with "Years" in the columns but here is a method using multiple measures. Feels like I am missing some of the data model here. See PBIX attached below sig.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler Thanks a lot. This is already a good step step forward. Using a measure for every year could work, but since I will be working with probably more than 100 years of data, I would have to create lots of measures and add all of them in another measure to get to the total. Working with just one measure, would of course be a lot easier, but I struggle to find a way to set it up. If you have an idea how that could be done, it would be fantastic...:-)

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.