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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
norambuena
Frequent Visitor

How to obtain the difference with previous data according to type and dates

Hello .
sorry but i'm new to this
I have the following problem...
I need to obtain the difference between the last value entered by Type and its previous value (of course, the date is not always the day before because there are weekends and holidays that are not entered)
Ex: 02-03-2022 Type A (6x6) -24 I have to subtract 02.02.2023 TYPE A (6X6) 185 , the same to TIPO B, TIPO C , etc....
And so for each tiop the same subtract the previous day and what to do when there are holidays without data of being Saturday or Sunday should take the value of Friday, in this case there would be a difference of 2 days with the previous datatipos.jpg

Calculate difference between current and previous row with a condition 

1 ACCEPTED SOLUTION
v-stephen-msft
Community Support
Community Support

Hi @norambuena ,

 

Based on your sample data, I created the following table. To simulate no input on Saturday and Sunday, I also added February 6th.

vstephenmsft_0-1675833829855.png

Below is the solution.

1.Group by.

vstephenmsft_1-1675834928498.png

vstephenmsft_2-1675835013213.png

vstephenmsft_4-1675835151174.png

2.Add a custom to return the index.

vstephenmsft_11-1675836936521.png

 

vstephenmsft_12-1675836965198.png

 

3.Dupliacte it to get Table 2. In Table 2, go to step of "Added Custom", replace the "0" with "1" as follows.

vstephenmsft_13-1675837018855.png

vstephenmsft_16-1675837085895.png

 

4.Expand them and get two following tables.

vstephenmsft_14-1675837043150.png

vstephenmsft_15-1675837050142.png

5.Merge them, set up as follows. Matching columns are Type and Index and note the numerical order of the them.

14.png

6.Expand the column after merged to get the previous value.

vstephenmsft_18-1675837438628.png

vstephenmsft_19-1675837472248.png

7.You can add a custom column to get the differences.

vstephenmsft_21-1675837574769.png

vstephenmsft_20-1675837527265.png

 

 

                                                                                                                                                         

Best Regards,

Stephen Tao

 

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

 

View solution in original post

2 REPLIES 2
v-stephen-msft
Community Support
Community Support

Hi @norambuena ,

 

Based on your sample data, I created the following table. To simulate no input on Saturday and Sunday, I also added February 6th.

vstephenmsft_0-1675833829855.png

Below is the solution.

1.Group by.

vstephenmsft_1-1675834928498.png

vstephenmsft_2-1675835013213.png

vstephenmsft_4-1675835151174.png

2.Add a custom to return the index.

vstephenmsft_11-1675836936521.png

 

vstephenmsft_12-1675836965198.png

 

3.Dupliacte it to get Table 2. In Table 2, go to step of "Added Custom", replace the "0" with "1" as follows.

vstephenmsft_13-1675837018855.png

vstephenmsft_16-1675837085895.png

 

4.Expand them and get two following tables.

vstephenmsft_14-1675837043150.png

vstephenmsft_15-1675837050142.png

5.Merge them, set up as follows. Matching columns are Type and Index and note the numerical order of the them.

14.png

6.Expand the column after merged to get the previous value.

vstephenmsft_18-1675837438628.png

vstephenmsft_19-1675837472248.png

7.You can add a custom column to get the differences.

vstephenmsft_21-1675837574769.png

vstephenmsft_20-1675837527265.png

 

 

                                                                                                                                                         

Best Regards,

Stephen Tao

 

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

 

alannavarro
Resolver I
Resolver I

Hello, hope it works.

let
//Source Data. I used as column names ID, Date, Material and Value.
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
// I have dates in english, is just a changed type to D/M/Y instead of M/D/Y.
#"Changed Type with Locale" = Table.TransformColumnTypes(Source, {{"Date", type date}}, "es-MX"),
#"Grouped Rows" = Table.Group(#"Changed Type with Locale", {"Material"}, {{"Count", each _, type table [ID=number, Date=nullable date, Material=text, Value=number]}}),
//Custom function to group all materials, sort them and then merge them with an index.
Custom =
(compare)=>
let
#"Sorted Rows" = Table.Sort( compare ,{{"Date", Order.Ascending}}),
#"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 1, 1, Int64.Type),
#"Added Index1" = Table.AddIndexColumn(#"Added Index", "Index.1", 0, 1, Int64.Type),
Custom1 = Table.NestedJoin(#"Added Index", "Index", #"Added Index1","Index.1", "Custom"),
#"Expanded Custom" = Table.ExpandTableColumn(Custom1, "Custom", {"Value"}, {"Value.1"}),
#"Replaced Value" = Table.ReplaceValue(#"Expanded Custom",null,0,Replacer.ReplaceValue,{"Value.1"}),
#"Removed Columns" = Table.RemoveColumns(#"Replaced Value",{"Index"})
in
#"Removed Columns",
Add_custom_column = Table.AddColumn(#"Grouped Rows", "Custom", each Custom([Count])),
#"Removed Other Columns" = Table.SelectColumns(Add_custom_column,{"Custom"}),
#"Expanded Custom" = Table.ExpandTableColumn(#"Removed Other Columns", "Custom", {"ID", "Date", "Material", "Value", "Value.1"}, {"ID", "Date", "Material", "Value", "Value.1"}),
#"Sorted Rows" = Table.Sort(#"Expanded Custom",{{"ID", Order.Ascending}})
in
#"Sorted Rows"

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors