Forum Discussion

ErikLe's avatar
ErikLe
New Member
5 years ago
Solved

Unpivot

Hi, I need some help with unpivot data in power query. At the moment this runs on SQL Server SSIS paket. In the future this will not be possible anymore. I hope, that you can help me. My star...
  • Anonymous's avatar
    Anonymous
    5 years ago
    let
    
        Origine = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bZBBDoUgDESvYlibn9IC6pIDeALC/a/xnQpaiAlM2jI8Sktx3q2O/O9aTIwEO0NOFYYI6kSqEervGIKcEUVIgmzk6locf6J5oC4v2T+5emHx+yXYhx4BChOxgcKXxfJeJrfMnvRaUlyYcbHjFkvTZubqPsSg4TKJoeFKDsOPdax9mMolac7Q3t+eXwOaZqiOMQ48M0bqBGm03urdbq1/", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [PK = _t, Date = _t, AG = _t, ART = _t, MA1 = _t, MA2 = _t, MA3 = _t, BA1 = _t, BA2 = _t, BA3 = _t, BS1 = _t, BS2 = _t, BS3 = _t, BZ1 = _t, BZ2 = _t, BZ3 = _t]),
        #"Intestazioni abbassate di livello" = Table.DemoteHeaders(Origine),
        #"Trasposta colonna" = Table.Transpose(#"Intestazioni abbassate di livello"),
        trtab=(tab)=> 
        let 
        t1=Table.ToColumns(Table.PromoteHeaders(Table.Transpose(tab)))
        in if tab[Column1]{0}="PK" then List.Combine(List.Repeat(t1,4)) else List.Combine(t1),
        #"Raggruppate righe" = Table.Group(#"Trasposta colonna", {"Column1"}, 
        {{"all", each trtab(_)}}, GroupKind.Local,(x,y)=>Number.From(Text.Start(x[Column1],2)<>Text.Start(y[Column1],2))),
    
        tfc=Table.FromColumns(#"Raggruppate righe"[all],#"Raggruppate righe"[Column1]),
        #"Ordinate righe" = Table.Sort(tfc,{{"PK", Order.Ascending}, {"Date", Order.Descending}}),
        #"Ricopiato in basso" = Table.FillDown(#"Ordinate righe",{"Date", "AG", "ART"})
    in
        #"Ricopiato in basso"