Forum Discussion
Sumarrize rows
- 5 years ago
Hi ThiagoRosa ,
Try below codes:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYgMgNoQSsTrRSkZQcSOQmDGShDFUAixmgiRhAmQ6wcRMkSRMoRJgMTOYRCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Type = _t, Start = _t, End = _t, Interval = _t]), #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Type", type text}, {"Start", Int64.Type}, {"End", Int64.Type}, {"Interval", Int64.Type}}), Custom1 = Table.TransformRows( #"Changed Type", each let mid = Table.Max(Table.SelectRows(#"Changed Type",(x)=>x[Type]=[Type]),"ID")[ID], res = if [ID]<>mid then let nextrow = Table.SelectRows(#"Changed Type",(y)=>y[ID]=[ID]+1) in if [Start]+[Interval]=List.Sum(nextrow[Start]) then let tab = Table.SelectRows(#"Changed Type",(z)=>z[ID]=[ID] or z[ID]=[ID]+1) in [ID=[ID],Type=[Type],Start=List.Min(tab[Start]),End=List.Max(tab[End]),Interval=List.Sum(tab[Interval])] else _ else [ID=null,Type=null,Start=null,End=null,Interval=null] in res ), #"Converted to Table" = Table.FromList(Custom1, Splitter.SplitByNothing(), null, null, ExtraValues.Error), #"Expanded Column1" = Table.ExpandRecordColumn(#"Converted to Table", "Column1", {"ID", "Type", "Start", "End", "Interval"}, {"ID", "Type", "Start", "End", "Interval"}), #"Filtered Rows" = Table.SelectRows(#"Expanded Column1", each ([ID] <> null)), #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"ID"}), #"Added Index" = Table.AddIndexColumn(#"Removed Columns", "ID", 1, 1, Int64.Type), #"Reordered Columns" = Table.ReorderColumns(#"Added Index",{"ID", "Type", "Start", "End", "Interval"}), #"Changed Type1" = Table.TransformColumnTypes(#"Reordered Columns",{{"ID", Int64.Type}, {"Type", type text}, {"Start", Int64.Type}, {"End", Int64.Type}}) in #"Changed Type1"And you will see:
You could also do it in dax,create 4 columns as below:
Column = var _nextstart=CALCULATE(MAX('Table (2)'[Start]),FILTER('Table (2)','Table (2)'[ID]=EARLIER('Table (2)'[ID])+1&&'Table (2)'[Type]=EARLIER('Table (2)'[Type]))) var _nextend=CALCULATE(MAX('Table (2)'[End]),FILTER('Table (2)','Table (2)'[ID]=EARLIER('Table (2)'[ID])+1&&'Table (2)'[Type]=EARLIER('Table (2)'[Type]))) var _previousstart=CALCULATE(MAX('Table (2)'[Start]),FILTER('Table (2)','Table (2)'[ID]=EARLIER('Table (2)'[ID])-1&&'Table (2)'[Type]=EARLIER('Table (2)'[Type]))) var _previousend=CALCULATE(MAX('Table (2)'[End]),FILTER('Table (2)','Table (2)'[ID]=EARLIER('Table (2)'[ID])-1&&'Table (2)'[Type]=EARLIER('Table (2)'[Type]))) Return IF(_nextstart<>BLANK()||_nextend<>BLANK(), IF('Table (2)'[Start]=_nextstart-'Table (2)'[Interval]&&'Table (2)'[End]=_nextend-'Table (2)'[Interval],1,0), IF(_nextstart=BLANK()||_nextend=BLANK(), IF('Table (2)'[Start]=_previousstart+'Table (2)'[Interval]&&'Table (2)'[End]=_previousend+'Table (2)'[Interval],1,0)))_Start = CALCULATE(MIN('Table (2)'[Start]),FILTER('Table (2)','Table (2)'[Column]=EARLIER('Table (2)'[Column])&&'Table (2)'[Type]=EARLIER('Table (2)'[Type])))_End = CALCULATE(MAX('Table (2)'[End]),FILTER('Table (2)','Table (2)'[Column]=EARLIER('Table (2)'[Column])&&'Table (2)'[Type]=EARLIER('Table (2)'[Type])))_Interval = CALCULATE(SUM('Table (2)'[Interval]),FILTER('Table (2)','Table (2)'[Column]=EARLIER('Table (2)'[Column])&&'Table (2)'[Type]=EARLIER('Table (2)'[Type])))And you will see:
For the related .pbix file,pls see attached.
Best Regards,
KellyDid I answer your question? Mark my post as a solution!
- Anonymous5 years ago
check this solution
edhans I will try to be more specific.
My original database corresponds to the road sections located in Brazil. Highways are named with the prefix "BR" associated with a corresponding number such as "101", for example. In addition, they have a beginning and an end. In my example, I assign the highway code as "Type", the start mileage as "start" and the end as "end". The "interval" corresponds to the difference between the "star" and the "end".
The original database is not well organized and I need to organize it so that it can generate maps.
In some situations, like the one described in my original message, I have two continuous excerpts in two rows from my table and, therefore, I need to combine them into a single record.
In the example I used, in situation 1, ID 3 was joined with ID 2 because it was a continuous stretch, that is, the "star" of ID 3 corresponds to the "end" of ID 2. Thus, ID 2 and ID 3 were merged, in Situation 2, with a new ID 2.
Something similar happened with ID 4 and 5. Since these are continuous stretches, they were condensed, in Situation 2, with a new ID 3.
Sincerely
I've played with it and still not sure I understand.
If 2 ends at 30, and 3 starts at 30, I get the connection, but since 3 ends at 40 and 4 starts at 40, why isn't that a continuation? Why did you start over with that part?