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

To celebrate FabCon Vienna, we are offering 50% off select exams. Ends October 3rd. Request your discount now.

Reply
mattwoldt
Frequent Visitor

Dynamically Sum between two datasets with multiple criteria

Hello,

 

Having a bit a pickle on this one, but I am hoping someone here can potentially help. I just am not really sure how I can do this dynamically, but I need to have power bi sum up information based off of two different data sets. 

So, we have revenue and the revenue is broken up by the ID of said person generating revenue, the month number, the year and the amount of revenue. 

On a different data set we have those same ID's where it will show a Start Month, Start Year and End Month and End Year. 

What I'm trying to do is to tell Dax to only sum up the information that is between that Start Month/Start Year and the End Month/End Year. 

 

For instance:

 

Bill made 1,000,000 in 2019 - 2020

and 1,000,000 in 2021-2022
the Break down is such:

 

IDRevenue YearRevenue MonthRevenue
12342019

3

250000
12342019

6

250000
12342020

2

250000
12342020

7

250000
12342021

1

250000
123420216250000
123420219250000
123420223250000

 

My other data set will look something like this:

IDStart MonthStart YearEnd MonthEnd YearRevenue Year
12346202062021Year 1
12346202162022Year 2

 

How can I tell dax to dynamically sum up between that time period. So essentially, what I am trying to do is create the below:

 

IDStart MonthStart YearEnd MonthEnd YearRevenue YearRevenue
12346202062021Year 1750,000
12346202162022Year 2500,000


Please know that there are over 4000 ID's and years spanning from 2013 all the way to 2034, so if I can possibly make this dynamic I would be so happy. 

Thank you again!

1 ACCEPTED SOLUTION

Hi @mattwoldt ,

 

Please take note that there is an overlap in your second table - 202106 is both the end period of Year 1 and start of Year 2.

 

To make the calcuation simple, I created several columns to help with the calculations.

Table1

Revenue YrMo = 
VALUE ( Table1[Revenue Year] & FORMAT ( Table1[Revenue Month], "00" ) )

Table2

Start YrMo = 
VALUE ( Table2[Start Year] & FORMAT ( Table2[Start Month], "00" ) )
End  YrMo = 
VALUE ( Table2[End Year] & FORMAT ( Table2[End Month], "00" ) )

 

For the revenue, you can either use a calculated column or a measure approach.

Calc column in table2

Revenue Sum (Calculated Column) = 
CALCULATE (
    SUM ( Table1[Revenue] ),
    FILTER (
        Table1,
        Table1[Revenue YrMo] >= EARLIER ( Table2[Start YrMo] )
            && Table1[Revenue YrMo] <= EARLIER ( Table2[End  YrMo] )
    ),
    Table1[ID] = EARLIER ( Table2[ID] )
)

Measure

Revenue Sum (Measure) = 
SUMX (
    ADDCOLUMNS (
        SUMMARIZE ( Table2, Table2[ID], Table2[Start YrMo], Table2[End  YrMo] ),
        "x",
            CALCULATE (
                SUM ( Table1[Revenue] ),
                FILTER (
                    Table1,
                    Table1[Revenue YrMo] >= [Start YrMo]
                        && Table1[Revenue YrMo] <= [End  YrMo]
                        && Table1[ID] = [ID]
                )
            )
    ),
    [x]
)

 

Those columns/measure would result to this:

danextian_0-1654563060682.png

Please refer to this link for the sample pbix - https://drive.google.com/file/d/1_Iv8UAqk58yxQOEDsLFdS7l3fgjxIUOh/view?usp=sharing 

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

9 REPLIES 9
Ashish_Mathur
Super User
Super User

Hi,

Show the exact result you are expecting.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Sorry, re-edited my post, didn't realize I had some typos. Essentially I am wanting to do a dynamic sum if that looks at the rows year and month. Essentially it is sum revenue that is equal to the start year and greater than or equal to the month # in the revenue table + revenue that is equal to the end year and less than or equal to the end month #. 

Please explain how you arrived at 750,000 and 500,000 very clearly.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
danextian
Super User
Super User

Hello @mattwoldt ,

What do you mean by this statement? There is no data below and the years in the second table are all 2020.

How can I tell dax to dynamically sum up between that time period. So essentiallyl where in the below data set we see Year 1 is between 2019 Month 6 and 2020 Month 6, this would sum up to $750,000 and then Year 2 would be $500,000.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Sorry, re-edited my post, didn't realize I had some typos. Essentially I am wanting to do a dynamic sum if that looks at the rows year and month. Essentially it is sum revenue that is equal to the start year and greater than or equal to the month # in the revenue table + revenue that is equal to the end year and less than or equal to the end month #. 

Hi @mattwoldt ,

 

Please take note that there is an overlap in your second table - 202106 is both the end period of Year 1 and start of Year 2.

 

To make the calcuation simple, I created several columns to help with the calculations.

Table1

Revenue YrMo = 
VALUE ( Table1[Revenue Year] & FORMAT ( Table1[Revenue Month], "00" ) )

Table2

Start YrMo = 
VALUE ( Table2[Start Year] & FORMAT ( Table2[Start Month], "00" ) )
End  YrMo = 
VALUE ( Table2[End Year] & FORMAT ( Table2[End Month], "00" ) )

 

For the revenue, you can either use a calculated column or a measure approach.

Calc column in table2

Revenue Sum (Calculated Column) = 
CALCULATE (
    SUM ( Table1[Revenue] ),
    FILTER (
        Table1,
        Table1[Revenue YrMo] >= EARLIER ( Table2[Start YrMo] )
            && Table1[Revenue YrMo] <= EARLIER ( Table2[End  YrMo] )
    ),
    Table1[ID] = EARLIER ( Table2[ID] )
)

Measure

Revenue Sum (Measure) = 
SUMX (
    ADDCOLUMNS (
        SUMMARIZE ( Table2, Table2[ID], Table2[Start YrMo], Table2[End  YrMo] ),
        "x",
            CALCULATE (
                SUM ( Table1[Revenue] ),
                FILTER (
                    Table1,
                    Table1[Revenue YrMo] >= [Start YrMo]
                        && Table1[Revenue YrMo] <= [End  YrMo]
                        && Table1[ID] = [ID]
                )
            )
    ),
    [x]
)

 

Those columns/measure would result to this:

danextian_0-1654563060682.png

Please refer to this link for the sample pbix - https://drive.google.com/file/d/1_Iv8UAqk58yxQOEDsLFdS7l3fgjxIUOh/view?usp=sharing 

 





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

I'm glad that worked.

EARLIER returns the value of a row from a column. If the second argument is not specified, that defaults to the current row.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Well I may have spoken a little too soon. I am getting calculations for some but for others it shows up as blank, any idea why? 
I checked the other data set the id's are definitely there and the ID's have revenue between those ranges. 

I'm not sure if I got it or not, but I tried trimming text and that seems to have fixed it so I think I am all good now. 

Wow!

I am absolutely amazed that is incredible. It 100% works. Thank you so much! So I guess the filter function with an earlier is a way to do a dynamic sum if it seems? Just absolutely incredible. 

Thank you so much. 

Helpful resources

Announcements
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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

Top Solution Authors