Forum Discussion

smpa01's avatar
smpa01
Community Champion
4 years ago
Solved

Bulk Renaming column in PQ with a pattern

AlexisOlson  ImkeF  whenever I pull data from SSAS tables, they come as like this.  I want to bulk rename the colNames in a way that returns the value inside parenthesis My end goal is t...
  • mahoneypat's avatar
    4 years ago

    I would try this way first, in a single rename columns step using List.Zip and List.Transform.

     

    let
      src=Table.FromRows(
        Json.Document(
          Binary.Decompress(
            Binary.FromText("i45WMlTSUTICYmOlWJ1oJRMgyxSIzZRiYwE=", BinaryEncoding.Base64),
            Compression.Deflate
          )
        ),
        let
          _t = ((type nullable text) meta [Serialized.Text = true])
        in
          type table [#"db[colA]" = _t, #"db[colB]" = _t, #"db[colC]" = _t]
      ),
        #"Renamed Columns" = Table.RenameColumns(src,List.Zip({Table.ColumnNames(src), List.Transform(Table.ColumnNames(src), each Text.BetweenDelimiters(_, "[", "]"))}))
    in
        #"Renamed Columns"

     

    Pat

  • AlexisOlson's avatar
    4 years ago

    I have a custom function ShortenColumnNames that I run on all of these:

    (Source as table) as table =>
    let
        ColNamesList = Table.ColumnNames(Source),
        Transform = Table.RenameColumns(Source,
            List.Transform(ColNamesList, each {_, Text.BetweenDelimiters(_, "[", "]")}))
    in
        Transform

     So each of my SSAS queries starts like this (I keep the DAX queries separately):

    let
        Source = AnalysisServices.Database(Workspace, Dataset, [Query=DAXQuery, Implementation="2.0"]),
        RenameCols = ShortenColumnNames(Source),
        [...etc...]