Forum Discussion

AjithTravvise's avatar
AjithTravvise
Helper II
6 years ago
Solved

Running total to date

Hello,

 

      I have the below data where the value is running total, need help in creating a a column where it will populate the date wise  

 

  • v-juanli-msft's avatar
    v-juanli-msft
    6 years ago

    Hi AjithTravvise 

    If the date increases daily, you could create calculated columns below:

    earlier =
    CALCULATE (
        SUM ( 'Table'[Running Value] ),
        FILTER (
            'Table',
            'Table'[Country]
                = EARLIER ( 'Table'[Country] )
                && 'Table'[Date]
                    = EARLIER ( 'Table'[Date] ) - 1
        )
    )
    
    daily total = [Running Value]-[earlier]
    
    

    As tested, if i understand you correctly, your second visual has some wrong data due to teh calculation rule.

     

    Best Regards
    Maggie
    Community Support Team _ Maggie Li
    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

  • Mariusz's avatar
    Mariusz
    6 years ago

    Hi AjithTravvise 

     

    This solution was created using Power Query as you have posted your question in PQ section, the script dose Include the M code in Added Column step of the script.

     

     

    Best Regards,
    Mariusz

    If this post helps, then please consider Accepting it as the solution.

    Please feel free to connect with me.
    LinkedIn

     

14 Replies

    • AjithTravvise's avatar
      AjithTravvise
      Helper II

      Greg,

       

          I have the below columes- Country/ Date/ Running value.... I wanted create a new column Daily figure, which will be the total value of that day. 

       

       

      CountryDateRunning ValueDaily Figure(need to be calculated)
      Japan1/20/2011
      India1/20/2055
      Japan1/21/2032
      India1/21/2050
      Japan1/22/2085
      • Mariusz's avatar
        Mariusz
        Community Champion

        Hi AjithTravvise 

         

        Please see the below script or attached file

        let
            Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8kosSMxT0lEyMjAy0DfUNzIAsg2VYnWilTzzUjIT0WRMwTLoegyBbGOsegxx6jECsi2UYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Country = _t, Date = _t, #"Running Value" = _t]),
            #"Changed Type" = Table.TransformColumnTypes(Source,{{"Country", type text}, {"Date", type date}, {"Running Value", Int64.Type}}),
            #"Grouped Rows" = Table.Group(#"Changed Type", {"Country"}, {{"tbl", each _, type table [Country=text, Date=date, Running Value=number]}}),
            #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each   
                let 
                    tbl = Table.Sort( [tbl], "Date" ),
                    count = Table.RowCount( tbl ),
                    lst = { 0..count -1 },
                    transform = List.Transform( lst, 
                        (i) => [ Value = try tbl{ i }[Running Value] - tbl{ i-1 }[Running Value] otherwise tbl{ i }[Running Value] ] & tbl{ i } 
                    )
                in 
                    transform
            ),
            #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"tbl"}),
            #"Expanded Custom" = Table.ExpandListColumn(#"Removed Columns", "Custom"),
            #"Expanded Custom1" = Table.ExpandRecordColumn(#"Expanded Custom", "Custom", {"Value", "Date", "Running Value"}, {"Value", "Date", "Running Value"}),
            #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Custom1",{{"Value", Int64.Type}, {"Date", type date}, {"Running Value", Int64.Type}})
        in
            #"Changed Type1"

         

        Best Regards,
        Mariusz

        If this post helps, then please consider Accepting it as the solution.

        Please feel free to connect with me.
        LinkedIn

         

    • AjithTravvise's avatar
      AjithTravvise
      Helper II

      Hi Greg,

       

          I tried all could, but stiil nothing worked, my request is very straight forward, i need to breakdown the incremental value on date wise in Daily Total Column. Appreciated your help

       

      CountryDateIncremental TotalDaily total
      Afghanistan1/20/202055
      Albania1/20/20201010
      Algeria1/20/202022
      Afghanistan1/21/2020105
      Albania1/21/20202010
      Algeria1/21/202020
      Afghanistan1/22/2020122
      Albania1/22/20204030
      Algeria1/22/202044
      Afghanistan1/23/2020153
      Albania1/23/20204212
      Algeria1/23/20201713
      Afghanistan1/24/2020227
      Albania1/24/20204230
      Algeria1/24/20202310
      Afghanistan1/25/20205028
      Albania1/25/20204313
      Algeria1/25/20202616
      • v-juanli-msft's avatar
        v-juanli-msft
        Community Support

        Hi AjithTravvise 

        If the date increases daily, you could create calculated columns below:

        earlier =
        CALCULATE (
            SUM ( 'Table'[Running Value] ),
            FILTER (
                'Table',
                'Table'[Country]
                    = EARLIER ( 'Table'[Country] )
                    && 'Table'[Date]
                        = EARLIER ( 'Table'[Date] ) - 1
            )
        )
        
        daily total = [Running Value]-[earlier]
        
        

        As tested, if i understand you correctly, your second visual has some wrong data due to teh calculation rule.

         

        Best Regards
        Maggie
        Community Support Team _ Maggie Li
        If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.