Forum Discussion

MadhavDholakia's avatar
2 years ago
Solved

Split Values in Power BI Query

Hello,   How to split below values in Power BI Query Editor?   Sample Values: col 1 1h 40m 56s 23m 45s 34s   Expected Results:  col 1, col 2, col 3 1, 40, 56 blank, 23, 45 blank, bla...
  • dufoq3's avatar
    dufoq3
    2 years ago

    Like this:

     

    Result

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMk5RMDTJUDAyzVUwNCxWitWJVjLMUDAxyFUwNYNwjYxzFUxMIWxjEyAdCwA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
        Ad_TotalSeconds = Table.AddColumn(Source, "TotalSeconds", each 
            [ a = {{"d", "*86400"}, {"h", "*3600"}, {"m", "*60"}, {"s", "*1"}},
              b = List.Accumulate( a, {}, (s,c)=> s & { if not Text.Contains([Column1], c{0}) then null else Text.Replace(List.Select(Text.Split([Column1], " "), (x)=> Text.Contains(x, c{0})){0}?, c{0}, c{1}) } ),
              c = List.Transform(b, (x)=> try Expression.Evaluate(x) otherwise null),
              d = List.Sum(c)
            ][d], Int64.Type),
        Ad_Splitted = Table.AddColumn(Ad_TotalSeconds, "Splitted", each 
            [ a = Text.SplitAny([Column1], "dhms"),
              b = List.Transform(List.Select(a, (x)=> Text.Trim(x) <> ""), Number.From),
              c = List.Repeat({null}, 4 - List.Count(b)) & b,
              d = Table.FromList({c}, (x)=> x, type table[Days=Int16.Type, Hours=Int64.Type, Minutes=Int64.Type, Seconds=Int64.Type])
            ][d], type table),
        ExpandedSplitted = Table.ExpandTableColumn(Ad_Splitted, "Splitted", {"Days", "Hours", "Minutes", "Seconds"})
    in
        ExpandedSplitted