Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
6 years ago
Solved

Get Previous Running Total for Missing Month

Hi All,

 

Let say I have two tables like below, I want to make accumulative line graph of which line should always increase.

The 'Date' table and 'ShipQty' table has relation in date column.

X axis: Date, Y axis: Qty

'Date'

Date
2001-01-01
2001-01-02
2001-01-03
~
2022-12-30
2022-12-31

 

'ShipQty' - (Qty is MTD value)

 

ItemDate  Qty
A2019-05-01  30
A2019-08-01  5
A2019-10-01  15
B2003-12-01  10
B2004-01-01  50
B2004-03-01  20
B2004-04-01  30
C2020-11-01  5
C2021-01-01  15
C2021-02-01  10

 

How can I get previous running total for missing month of each item?

The result should be like below.

 

'Running Total' - (Should not have missing month.)

The records in red should be inserted with previous qty and missing month.

The Qty values in blue should be the result of sum of previous value and current month. 

ItemDate  Qty
A  2019-05-01  30
A  2019-06-01  30(Previous value)
A  2019-07-01  30(Previous value)
A  2019-08-01  35(30+5)
A  2019-09-01  35(Previous value)
A  2019-10-01  50(35+15)
B  2003-12-01  10
B  2004-01-01  60(10+50)
B  2004-02-01  60(Previous value)
B  2004-03-01  80(20+60)
B  2004-04-01  110(30+80)
C  2020-11-01  5
C  2020-12-01  5(Previous value)
C  2021-01-01  20(15+5)
C  2021-02-01  30(10+20)

 

Please kindly help me.

 

Thanks in advance.

 

  • dax's avatar
    dax
    6 years ago

    Hi Anonymous , 

    You could create a calendat table (2019/1/~2022/12/21)

    calendar = CALENDAR(date(2019,1,1), date(2022,12,31))

    then create relationship between calendar and fact table, and use measure and calendar in table like below, 

    Measure = var  maxd=CALCULATE(MAX('Table'[Date]), ALLEXCEPT('Table','Table'[Item]))  return CALCULATE(SUM('Table'[  Qty]), FILTER(ALLSELECTED('calendar'), 'calendar'[Date]<=MIN('calendar'[Date]) && MIN('calendar'[Date])<=maxd))

     

     It will show all months in each year, and end with the max month in fact table. You could refer to my sample for details.

    Best Regards,
    Zoe Zhi

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

     

4 Replies

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thanks VijayP 

       

      I checked your file but one thing that I wonder is how could you make every month in "table".

      Because in my case, there is no row for missing value, even month value.

      The hard thing is insert missing month row if it is missed.

       

      For example my table looks below, I should create April.

      (Sorry for Date language...)

      • dax's avatar
        dax
        Icon for Community Support rankCommunity Support

        Hi Anonymous , 

        You could create a calendat table (2019/1/~2022/12/21)

        calendar = CALENDAR(date(2019,1,1), date(2022,12,31))

        then create relationship between calendar and fact table, and use measure and calendar in table like below, 

        Measure = var  maxd=CALCULATE(MAX('Table'[Date]), ALLEXCEPT('Table','Table'[Item]))  return CALCULATE(SUM('Table'[  Qty]), FILTER(ALLSELECTED('calendar'), 'calendar'[Date]<=MIN('calendar'[Date]) && MIN('calendar'[Date])<=maxd))

         

         It will show all months in each year, and end with the max month in fact table. You could refer to my sample for details.

        Best Regards,
        Zoe Zhi

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