Forum Discussion

ahoggatt42's avatar
ahoggatt42
Regular Visitor
2 years ago
Solved

Cumulative Sum from Two Different Tables

My company is currently using two completely seperate systmes to track sales in US vs sales for the rest of the world. Additionally we have two different types of sales, OP and UO orders. I am trying to create a cumulative sum that takes the data from both tables by day and does a grand total sum.

Here is what I have written so far. AR is one table and SO is a seperate table. They are both joined to a "Bookings Goal" Table by their dates. The HistDetailAll Table is joined by the Order Date and the TransDetailAll Table is joined by the TransDate.

Intl Cumulative UO Bookings (Month) =

var AR =
CALCULATE(
    SUM(HistDetailAll[True Price]),
    FILTER(
        ALL(HistDetailAll),
        HistDetailAll[Order Date]<= MAX( HistDetailAll[Order Date]) &&
        HistDetailAll[Order Month] = MAX(HistDetailAll[Order Month])&&
        HistDetailAll[Order Year] = MAX(HistDetailAll[Order Year])&&
        HistDetailAll[Order Type] = "UO"
        ))
var SO =
CALCULATE(
    SUM(TransDetailAll[True Price]),
    FILTER(
        ALL(TransDetailAll),
        TransDetailAll[Trans Date] <= MAX( TransDetailAll[Trans Date]) &&
        TransDetailAll[Trans Month] = MAX(TransDetailAll[Trans Month])&&
        TransDetailAll[Trans Year] = MAX(TransDetailAll[Trans Year])&&
        TransDetailAll[Order Type] = "UO"
        ))


Return
AR+SO
 
This same code is repeated for the OP orders just change the "..."[Order Type] = "UO" to "..."[Order Type] = "OP"
 
This is what the table looks like after running this though

Ive used this exact formula in other instances and gotten the results I was expecting. Im imagining this has to do with the fact that I am trying to pull from two different tables to get this result.

 

Any thoughts on what Im doing wrong here? Im hiting a wall.

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi ahoggatt42 ,

    1. Create a calculation table and obtain data that meets the conditions.

    Table = 
    UNION(SELECTCOLUMNS(
        FILTER('AR Data','AR Data'[Order Type] = "UO"),
        "MyDate",'AR Data'[Order Date],
        "MyYear",'AR Data'[Order Year],
        "MyMonth",'AR Data'[Order Month],
        "MyPrice",'AR Data'[Sum of True Price]),
        SELECTCOLUMNS(
            FILTER('SO Data','SO Data'[Order Type] = "UO"),
            "MyDate",'SO Data'[Trans Date],
            "MyYear",'SO Data'[Trans Year],
            "MyMonth",'SO Data'[Trans Month],
            "MyPrice",'SO Data'[Sum of True Price]))

     

    2. Create measure.

    Measure =
    CALCULATE(SUM('Table'[MyPrice]),FILTER(ALL('Table'), 'Table'[MyYear] = MAX('Table'[MyYear]) && 'Table'[MyMonth] = MAX('Table' [MyMonth]) &&'Table'[MyDate] <= MAX('Table'[MyDate])))

    If your Current Period does not refer to this, please clarify in a follow-up reply.

     

    Best Regards,

    Clara Gong

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

6 Replies

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi ahoggatt42 ,

    Based on your description, I created three tables and created relationships for them.

     

    Subsequently a measure similar to the one in your description was written and it gave the correct results.

    Measure =
    VAR test1 =
    CALCULATE(SUM('Table A'[Price]),FILTER(ALL('Table A'),'Table A'[date] = MAX('Table A'[date]) && 'Table A'[Type] = "A"))
    VAR test2 =
    CALCULATE(SUM('Table B'[Price]),FILTER(ALL('Table B'),'Table B'[date] = MAX('Table B'[date]) && 'Table B'[Type] = "A"))
    RETURN
    test1 + test2

     

    Therefore, please confirm whether the direct relationship between your tables is correct. Another thing to note is that the date of the intermediate table should be placed in the visual object, which is the Bookings Goal table in your description.

     

    Alternatively, there is a way to get the results you want, which requires building a calculation sheet.

    Table =
    UNION(SELECTCOLUMNS(
    FILTER('Table A','Table A'[Type] = "A"),
    "MyDate",'Table A'[date],
    "MyPrice",'Table A'[Price]),
    SELECTCOLUMNS(
    FILTER('Table B','Table B'[Type] = "A"),
    "MyDate",'Table B'[date],
    "MyPrice",'Table B'[Price]))

     

    Then create measure and perform summation.

    Measure 2 =
    CALCULATE(SUM('Table'[MyPrice]),FILTER('Table','Table'[MyDate] = MAX('Table'[MyDate])))

    If your Current Period does not refer to this, please clarify in a follow-up reply.

     

    Best Regards,

    Clara Gong

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

    • ahoggatt42's avatar
      ahoggatt42
      Regular Visitor

      Thanks for your reply. I think I need to clarify what my expected outcome is. If I use your sample (For simplicity) I would expect something like this:

      1/1/2024  20
      1/2/2024  31
      1/3/2024  64
      1/5/2024 125
      1/6/2024 140

       

      Based on what you have on your relationships that is how I have mine set up as well. I can put together some samples of my data if that would help.

  • ahoggatt42's avatar
    ahoggatt42
    Regular Visitor

    Just to go one step further here is a sample of the data I am using.

    This is the SO Table:

    TransIDItemIdTrans DateTrans MonthTrans YearSum of True PriceOrder Type
    00001041273/1/2024 0:0032024$351,574UO
    00001281283/1/2024 0:0032024$78,339UO
    0001099533/4/2024 0:0032024$43,800OP
    0001099573/4/2024 0:0032024$1,692OP
    0001099593/4/2024 0:0032024$4,909OP
    00010995183/4/2024 0:0032024$159OP
    0001100063/7/2024 0:0032024$598OP
    00011001223/7/2024 0:0032024$4,111OP
    00000298233/18/2024 0:0032024$99,450UO
    00011017103/18/2024 0:0032024$126OP
    00011021103/19/2024 0:0032024$75OP
    00011035163/22/2024 0:0032024$160OP
    0001103683/22/2024 0:0032024$883OP
    00001042153/25/2024 0:0032024$93,600UO
    00001042253/25/2024 0:0032024$13,985UO
    00001042263/25/2024 0:0032024$76,290UO
    0001103773/25/2024 0:0032024$3,138OP
    00011037113/25/2024 0:0032024$127OP
    00011037123/25/2024 0:0032024$215OP
    00011037133/25/2024 0:0032024$585OP
    00011037143/25/2024 0:0032024$886OP
    00011037183/25/2024 0:0032024$1,590OP
    00011037193/25/2024 0:0032024$1,877OP
    00011037213/25/2024 0:0032024$3,513OP
    00001043243/26/2024 0:0032024$274,560UO
    0001104813/27/2024 0:0032024$1,663OP
    0001104823/27/2024 0:0032024$625OP
    0001104833/27/2024 0:0032024$1,218OP
    0001104843/27/2024 0:0032024$5,884OP
    0001104853/27/2024 0:0032024$1,053OP
    00011048103/27/2024 0:0032024$50OP
    00011048173/27/2024 0:0032024$26OP
    00011048203/27/2024 0:0032024$13OP

     

  • ahoggatt42's avatar
    ahoggatt42
    Regular Visitor

    And Here is the AR Data:

    transidpartidOrder DateOrder MonthOrder YearSum of True PriceOrder Type
    00010993293/4/2024 0:0032024$68OP
    0001099613/5/2024 0:0032024$665OP
    00010998513/5/2024 0:0032024$98OP
    00010998533/5/2024 0:0032024$2,118OP
    00010999303/6/2024 0:0032024$68OP
    00010999333/6/2024 0:0032024$14,265OP
    0001099923/6/2024 0:0032024$568OP
    00010999373/6/2024 0:0032024$85OP
    00010999103/6/2024 0:0032024$478OP
    00010999523/6/2024 0:0032024$34OP
    00001282633/6/2024 0:0032024$26OP
    00010999643/6/2024 0:0032024$12OP
    00001282663/6/2024 0:0032024$105OP
    00010999173/6/2024 0:0032024$78OP
    00010999713/6/2024 0:0032024$2,888OP
    0001100233/7/2024 0:0032024$6,088OP
    00011002393/7/2024 0:0032024$554OP
    00011002403/7/2024 0:0032024$223OP
    00011002413/7/2024 0:0032024$602OP
    00011002423/7/2024 0:0032024$101OP
    00011002433/7/2024 0:0032024$453OP
    00011002443/7/2024 0:0032024$640OP
    00011002453/7/2024 0:0032024$101OP
    00011002493/7/2024 0:0032024$109OP
    00011002543/7/2024 0:0032024$25OP
    00011002563/7/2024 0:0032024$97OP
    00011002573/7/2024 0:0032024$11OP
    00011002583/7/2024 0:0032024$24OP
    00011002623/7/2024 0:0032024$19OP
    00011002683/7/2024 0:0032024$703OP
    00011002693/7/2024 0:0032024$11OP
    00011002703/7/2024 0:0032024$17OP
    00011001223/7/2024 0:0032024$13,019OP
    00011002723/7/2024 0:0032024$6,108OP
    00011004343/11/2024 0:0032024$43OP
    00011004363/11/2024 0:0032024$94OP
    00011008323/12/2024 0:0032024$665OP
    00011009673/12/2024 0:0032024$831OP
    00011015293/15/2024 0:0032024$68OP
    00011015373/15/2024 0:0032024$227OP
    00011015463/15/2024 0:0032024$603OP
    00011015593/15/2024 0:0032024$25OP
    00011015603/15/2024 0:0032024$73OP
    00011015613/15/2024 0:0032024$85OP
    00011015223/15/2024 0:0032024$343OP
    00011021103/19/2024 0:0032024$50OP
    00011021483/19/2024 0:0032024$112OP
    00011024293/20/2024 0:0032024$68OP
    0001102423/20/2024 0:0032024$57OP
    00011024353/20/2024 0:0032024$533OP
    00011024503/20/2024 0:0032024$3,267OP
    00011024533/20/2024 0:0032024$1,059OP
    00011024653/20/2024 0:0032024$6,460OP
    00011024203/20/2024 0:0032024$39OP
    00011036313/22/2024 0:0032024$919OP
    00011036553/22/2024 0:0032024$813OP
    00001044243/26/2024 0:0032024$274,560UO
    00011043383/27/2024 0:0032024$516OP
    00011043473/27/2024 0:0032024$209OP
    0001104913/28/2024 0:0032024$998OP
  • ahoggatt42's avatar
    ahoggatt42
    Regular Visitor

    I also created a calendar table (Bookings Goal)

     

    I joined all three tables based on their dates

     

    And using the same measure I used before:

    Intl Cumulative UO Bookings (Month) =

    var AR =
    CALCULATE(
        SUM('AR Data'[Sum of True Price]),
        FILTER(
            ALL('AR Data'),
            'AR Data'[Order Date]<= MAX( 'AR Data'[Order Date]) &&
            'AR Data'[Order Month] = MAX('AR Data'[Order Month])&&
            'AR Data'[Order Year] = MAX('AR Data'[Order Year])&&
            'AR Data'[Order Type] = "UO"
            ))
    var SO =
    CALCULATE(
        SUM('SO Data'[Sum of True Price]),
        FILTER(
            ALL('SO Data'),
            'SO Data'[Trans Date] <= MAX( 'SO Data'[Trans Date]) &&
            'SO Data'[Trans Month] = MAX('SO Data'[Trans Month])&&
            'SO Data'[Trans Year] = MAX('SO Data'[Trans Year])&&
            'SO Data'[Order Type] = "UO"
            ))


    Return
    SO+AR

    I get this result:

     


    I am expecting that each column is a running sum by date.

    I have this working for just one location now without any issues using the same code just without adding another site to it:

     

    Intl Cumulative UO Revenue (Month) =
    CALCULATE(
        SUM(HistDetailAll[True Price]),
        FILTER(
            ALL(HistDetailAll),
            HistDetailAll[actshipdate]<= MAX( HistDetailAll[actshipdate]) &&
            HistDetailAll[Actual Month] = MAX(HistDetailAll[Actual Month])&&
            HistDetailAll[Actual Year] = MAX(HistDetailAll[Actual Year]) &&
            HistDetailAll[Order Type] = "UO"
            ))

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi ahoggatt42 ,

    1. Create a calculation table and obtain data that meets the conditions.

    Table = 
    UNION(SELECTCOLUMNS(
        FILTER('AR Data','AR Data'[Order Type] = "UO"),
        "MyDate",'AR Data'[Order Date],
        "MyYear",'AR Data'[Order Year],
        "MyMonth",'AR Data'[Order Month],
        "MyPrice",'AR Data'[Sum of True Price]),
        SELECTCOLUMNS(
            FILTER('SO Data','SO Data'[Order Type] = "UO"),
            "MyDate",'SO Data'[Trans Date],
            "MyYear",'SO Data'[Trans Year],
            "MyMonth",'SO Data'[Trans Month],
            "MyPrice",'SO Data'[Sum of True Price]))

     

    2. Create measure.

    Measure =
    CALCULATE(SUM('Table'[MyPrice]),FILTER(ALL('Table'), 'Table'[MyYear] = MAX('Table'[MyYear]) && 'Table'[MyMonth] = MAX('Table' [MyMonth]) &&'Table'[MyDate] <= MAX('Table'[MyDate])))

    If your Current Period does not refer to this, please clarify in a follow-up reply.

     

    Best Regards,

    Clara Gong

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.