Forum Discussion

mattwoldt's avatar
mattwoldt
Frequent Visitor
4 years ago
Solved

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!

  • 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:

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

     

9 Replies

  • 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.

    • mattwoldt's avatar
      mattwoldt
      Frequent Visitor

      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 #. 

      • danextian's avatar
        danextian
        Super User

        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:

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

         

    • mattwoldt's avatar
      mattwoldt
      Frequent Visitor

      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 #. 

      • Ashish_Mathur's avatar
        Ashish_Mathur
        Super User

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