Forum Discussion
Extracting a hierarchy from a range
Hi,
this row:
filter = Table.Buffer(Table.SelectRows(Source, each Text.StartsWith([Name], "M"))),
It assumes headlines starts with "M", but it can start with any letter.
I tested with part of my table. Unfortunately result is not as I intented.
Se code below and my result.
Maybe it is caused my bad description.
It seems I get ALL parents in a row. But in reverse order.
I think problem is that a leaf can be in several branches.
It is maybe just not possible to get it the way I want (full hierachy for each record on each row).
So maybe you could hal with a more simple solution.
1. Search should be done for each row regardless of if it has a range or if it is "single/leaf"
2. Search should be done from last row upwards in table. ( Table is sorted correct)
3. First occurance of record with range is regarded parent
4. Table is extended with Parent Name, Number and Range
This is code I tried (I tried both versions with similar results). In it you can find my more authetic data.
let
Source1 = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fVNBcoIwFL1KxnWZIorVJQ7IUC2dQezGcRFNYKIWOwG8QT2DJ/AM3edizQ8gkZl29V4m7+W/n/ys170gI8WeZixL8y0jHKe9p17fNE0Jw8HEqleG4pundc9JDHRKUUZJfuDsDEbKEU4Qzo7ipruHk9HdDRzcEc3LY4ELlIgfjjjNCso1i23bdwtwZfGWq0XsxGgmrhFazp1YM4wVVAbgYBDfnBa5PLwqpaknw8ldDRzUKwJRCoT5lu4Jhf1+3+wACN/fluISh0Hoo8jzAZdONPVeXU8XDxqPofijURNa41YIHISuuMzVudPAjRxNbZkvd7XinTxxsJiu3Ic4o8pQVxlBnLpKzI7bkuR1vxwZCD/vdHELIA9SnKVncZNvRSjSXF+cfZ5UK3VHLSif74T+h7hEXuh6qI4W6UKr0RuK/1tMvmRTzTI7oKYydCMPBaEbi4sf13UGtnbTctXetJOkLJGjB5NcsGMzuNbQ7ICapyunOOFlRmDY1Y5tdgB0c5znlLAkoTL+TsVm8nPJs9UMWtWTaPCHqSQQTomqZ9cALDN8KEqOOd7Kf2SggwxWfaKm0xY2m18=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Number = _t, Endnumber = _t, Range = _t]),
Source = Table.TransformColumnTypes(Source1,{{"Name", type text}, {"Number", Int64.Type}, {"Endnumber", Int64.Type}, {"Range", type text}}),
A = Table.AddColumn(Source,"m",each
let
a=Table.ToRows(Table.SelectRows(Table.FirstN(Source,List.PositionOf(Source[Name],[Name])),(x)=>x[Range]<>null)),
b=List.Transform(a,(y)=>if [Number]<=y{2} and [Number]>=y{1} then List.RemoveRange(y,2,1) else {})
in
List.RemoveRange(Record.ToList(_),2,1)&List.Combine(b)
),
Result = Table.FromList(A[m],each _,List.Count(List.Last(A[m])))
in
Result
- Aerobat5 years agoFrequent Visitor
Code in better format
let Source1 = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fVNBcoIwFL1KxnWZIorVJQ7IUC2dQezGcRFNYKIWOwG8QT2DJ/AM3edizQ8gkZl29V4m7+W/n/ys170gI8WeZixL8y0jHKe9p17fNE0Jw8HEqleG4pundc9JDHRKUUZJfuDsDEbKEU4Qzo7ipruHk9HdDRzcEc3LY4ELlIgfjjjNCso1i23bdwtwZfGWq0XsxGgmrhFazp1YM4wVVAbgYBDfnBa5PLwqpaknw8ldDRzUKwJRCoT5lu4Jhf1+3+wACN/fluISh0Hoo8jzAZdONPVeXU8XDxqPofijURNa41YIHISuuMzVudPAjRxNbZkvd7XinTxxsJiu3Ic4o8pQVxlBnLpKzI7bkuR1vxwZCD/vdHELIA9SnKVncZNvRSjSXF+cfZ5UK3VHLSif74T+h7hEXuh6qI4W6UKr0RuK/1tMvmRTzTI7oKYydCMPBaEbi4sf13UGtnbTctXetJOkLJGjB5NcsGMzuNbQ7ICapyunOOFlRmDY1Y5tdgB0c5znlLAkoTL+TsVm8nPJs9UMWtWTaPCHqSQQTomqZ9cALDN8KEqOOd7Kf2SggwxWfaKm0xY2m18=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, Number = _t, Endnumber = _t, Range = _t]), Source = Table.TransformColumnTypes(Source1,{{"Name", type text}, {"Number", Int64.Type}, {"Endnumber", Int64.Type}, {"Range", type text}}), A = Table.AddColumn(Source,"m",each let a=Table.ToRows(Table.SelectRows(Table.FirstN(Source,List.PositionOf(Source[Name],[Name])),(x)=>x[Range]<>null)), b=List.Transform(a,(y)=>if [Number]<=y{2} and [Number]>=y{1} then List.RemoveRange(y,2,1) else {}) in List.RemoveRange(Record.ToList(_),2,1)&List.Combine(b) ), Result = Table.FromList(A[m],each _,List.Count(List.Last(A[m]))) in Result- shaowu4595 years agoResolver II
In order to better understand your case, could you post a picture of the expected result of below table?
- Aerobat5 years agoFrequent Visitor
I think it should be something like this:
Name Number Endnumber Range Parent Parent Number Parent Endnumber Parent Range Årets resultat 1000 4949 1000-4949 RESULTAT FØR SKAT 1000 4800 1000-4800 Årets resultat 1000 4949 1000-4949 Resultat før renter 1000 4555 1000-4555 RESULTAT FØR SKAT 1000 4800 1000-4800 Af- og nedskrivninger af anlæg 1000 4496 1000-4496 Resultat før renter 1000 4555 1000-4555 Indtjeningsbidrag 1000 4392 1000-4392 Af- og nedskrivninger af anlæg 1000 4496 1000-4496 DÆKNINGSBIDRAG 1110 2070 1110-2070 Indtjeningsbidrag 1000 4392 1000-4392 OMSÆTNING 1110 1280 1110-1280 DÆKNINGSBIDRAG 1110 2070 1110-2070 OMSÆTNING REGNINGSARBEJDE 1110 1130 1110-1130 OMSÆTNING 1110 1280 1110-1280 Udført arbejde 1110 1110 1110 OMSÆTNING REGNINGSARBEJDE 1110 1130 1110-1130 OMSÆTNING TILBUDSARBEJDE 1160 1180 1160-1180 OMSÆTNING 1110 1280 1110-1280 Tilbudsarbejder - a/c 1180 1180 1180 OMSÆTNING TILBUDSARBEJDE 1160 1180 1160-1180 IGANGVÆRENDE ARBEJDER 1210 1220 1210-1220 OMSÆTNING 1110 1280 1110-1280 Igangværende arbejder - primo 1210 1210 1210 IGANGVÆRENDE ARBEJDER 1210 1220 1210-1220 Igangværende arbejder - ultimo 1220 1220 1220 IGANGVÆRENDE ARBEJDER 1210 1220 1210-1220 ANDRE INDTÆGTER 1235 1280 1235-1280 OMSÆTNING 1110 1280 1110-1280 Afgifter og tillæg 1240 1240 1240 ANDRE INDTÆGTER 1235 1280 1235-1280 Øreafrundning 1250 1250 1250 ANDRE INDTÆGTER 1235 1280 1235-1280 Kassedifferencer - indtægt 1260 1260 1260 ANDRE INDTÆGTER 1235 1280 1235-1280 Kassedifferencer - udgift 1270 1270 1270 ANDRE INDTÆGTER 1235 1280 1235-1280 Fakturarabat - kunder 1280 1280 1280 ANDRE INDTÆGTER 1235 1280 1235-1280