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

Get certified in Microsoft Fabric—for free! For a limited time, the Microsoft Fabric Community team will be offering free DP-600 exam vouchers. Prepare now

Reply
dlabards
New Member

How to Add Column to get the difference of sold items from current month to previous month

I want to add a column to get the monthly consumed product based on the cumulative reading. below is the data and the expected output (Consumed Monthly)

 

dlabards_0-1731380705107.png

 

8 REPLIES 8
Fowmy
Super User
Super User

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Grouped Rows" = Table.Group(Source, {"Display Name"}, 
        {{"Details", 
            (t)=> Table.AddColumn( 
                Table.AddIndexColumn( t, "i",0,1) , 
                "Consumed" , 
                    each try t[Cumulative Meter Reading]{[i]} - t[Cumulative Meter Reading]{[i]-1}  otherwise 0  )  
         }
        }
        )
in
   Table.Combine(#"Grouped Rows"[Details])
Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

shafiz_p
Memorable Member
Memorable Member

Hi @dlabards Try below codes:

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type datetime}, {"Display Name", type text}, {"Cumulative Meter Reading", Int64.Type}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Changed Type",{{"Date", type date}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type1", "Index", 1, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Previous Month", each try if [Index] = 1 or [Display Name] <> Table.AddIndexColumn(#"Changed Type1", "Index", 1, 1, Int64.Type)[Display Name]{[Index]-2} then null else Table.AddIndexColumn(#"Changed Type1", "Index", 1, 1, Int64.Type)[Cumulative Meter Reading]{[Index]-2} otherwise null),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Consumed Monthly", each if [Previous Month] = null then null else [Cumulative Meter Reading] - [Previous Month], Int64.Type),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom1",{"Index", "Previous Month"})
in
    #"Removed Columns"

 

Output:

shafiz_p_0-1731382195199.png

 

 

Hope this helps!!

If this solved your problem, please accept it as a solution and a kudos!!

 

Best Regards,
Shahariar Hafiz

@shafiz_p where should i paste this code? apologies as i'm a newbie in powerbi

Open power query > go to Home tab > open advance editor > replace all by the given codes.
See image:

shafiz_p_0-1731388915003.png

 

Hope this helps!!

thanks a lot! it is now working and trying to incorporate in my data

hello, i'm getting a wrong output when i applied to my datasource. hope you could share the details of the code below

 #"Added Custom" = Table.AddColumn(#"Added Index", "Previous Month", each try if [Index] = 1 or [Display Name] <> Table.AddIndexColumn(#"Changed Type1", "Index", 1, 1, Int64.Type)[Display Name]{[Index]-2} then null else Table.AddIndexColumn(#"Changed Type1", "Index", 1, 1, Int64.Type)[Cumulative Meter Reading]{[Index]-2} otherwise null),

 

AlienSx
Super User
Super User

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content], 
    rows = List.Buffer(Table.ToList(Source, (x) => x)),
    gen = List.Generate(
        () => [i = 0, r = rows{0}, consumed = null],
        (x) => x[i] < List.Count(rows),
        (x) => [i = x[i] + 1, r = rows{i}, consumed = if r{1} <> x[r]{1} then null else r{2} - x[r]{2}],
        (x) => x[r] & {x[consumed]}
    ), 
    result = Table.FromList(gen, (x) => x, Table.ColumnNames(Source) & {"Consumed Monthly"})
in
    result

thank you for the reply but i'm a newbie in power bi. appreciate if this can be translated to a layman's term please

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.

October NL Carousel

Fabric Community Update - October 2024

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