• Zur Hauptnavigation springen
  • Zum Inhalt springen
  • Zur Seitenspalte springen
  • Zur Fußzeile springen

THE SELF-SERVICE-BI BLOG

Wir lieben Microsoft Power BI

  • Videokurse
    • Coming soon!
  • Live Online Power BI-Schulungen
  • Beratung
    • Was ich für Dich tun kann
    • Showcases
  • Kunden
  • BLOG
    • Business Topics
    • Technical Topics (english)
    • Tools
  • Podcast
  • Kontakt
  • Über

Technical Topics (english) / 3. September 2020

Fast table construction with Table.FromColumns and lists with metadata records

One of the fastest methods I know to create tables in Power Query/ M is the function Table.FromColumns(), which takes a list of lists and creates a table from them. This way you can easily create a calendar table for example. In this article I will show you how you can use the metadata record of list items to flexibly assign both column names and datatypes to the created table.

Take a look at the following M script. At the beginning I define 4 lists: A list of…

  • dates,
  • day names,
  • month names and
  • month numbers.

The ListOfColumns is basically a compilation of the lists defined above, which are to be included in the final table. Here I can flexibly determine 1) if they should be included and 2) in which order they should be placed.

The final Step GetTable creates a table from the lists defined in ListOfColumns.

The problem with this solution is that the final table has neither data types nor column names.

Final table WITHOUT proper column names and data types, Power Query, Power BI, Excel
Final table WITHOUT proper column names and data types

A possible solution is to define in the ListOfColumns list not only the lists to be included in the table, but also the column name that the list is to receive in the table and the later data type. I define this via the metadata record (To be seen in code lines 16 to 23):

In line 28 I use the Value.Metadata() function to retrieve the ColName information from the metadata record of each list item and get a list of column names.

In line 31 I not only create the table, but also use the second argument of the Table.FromColumn function to define the correct column names.

From row 34 on I assign the correct data types to the columns, which I had stored in the metadata record of the list items. The result is a table that can be created flexibly and that shows both column name and correct data types:

Final table with proper column names and data types, Power Query, Power BI, Excel
Final table with proper column names and data types

Cheers from Hamburg, Germany

Lars

Lars Schreiber

Lars ist Berater, Entwickler und Trainer für Microsoft Power BI. Er ist zertifizierter Power BI-Experte und Microsoft Trainer. Für sein Engagement in der internationalen Community wurde Lars seit 2017 jährlich durch Microsoft der MVP-Award verliehen. Lies hier mehr…

Kategorie: Technical Topics (english) Stichworte: metadata, Power Query, Table.FromColumns

Leser-Interaktionen

Kommentare

  1. Jan Willem van Holst meint

    20. Januar 2021 um 12:28 pm

    Lars,
    Thx! Why recalculate the table names in line 38, while they are still available in ListOfColNames?

    • Lars Schreiber meint

      20. Januar 2021 um 1:13 pm

      Hi Jan,

      I would love to tell you, that it is about performance or at least elegancy of the code, but the hard truth is: I messed up 😛 Thanks for improving the code. I updated it.

      Cheers,
      Lars

  2. Alvaro meint

    9. September 2020 um 4:21 am

    Hello Lars,
    I have read several of your publications and lately your podcast.
    Thank you

    • Lars Schreiber meint

      9. September 2020 um 7:27 am

      Hello Alvaro,

      really happy to ‚hear‘ that.

      Thanks for your feedback 🙂
      Lars

  3. Konrad meint

    4. September 2020 um 1:16 pm

    Really handy solution. I’m curious though why you use List.Buffer for columns (DayName,etc). I understand the use for Date, since that is re-used multiple times but why for the other columns?

    • Lars Schreiber meint

      4. September 2020 um 1:23 pm

      Hi Konrad,

      I wouldn’t get too hung up on that. I copied this example from another solution. In this context the List.Buffer() really makes little sense 🙂

      Thanks and cheers,
      Lars

Seitenspalte

WEBSITE DURCHSUCHEN

MELDE DICH FÜR DIE POWER BI USER GROUP HAMBURG AN

Trage Deine E-Mailadresse ein, um für kommende Treffen der PUG Hamburg eingeladen zu werden. Zudem erhältst Du Zugriff auf die Materialien der vergangenen Treffen.

Footer

Kontakt

THE SELF-SERVICE-BI BLOG
Mail: lars@ssbi-blog.de

Rechtliches

  • Impressum
  • Datenschutz

Über THE SELF-SERVICE-BI BLOG

Ich bin ein freiberuflicher Power BI-Berater, -Entwickler und -Trainer und wurde von Microsoft mehrfach mit dem MVP Award ausgezeichnet. Ich arbeite mit einem kompetenten Netzwerk aus freiberuflichen und ambitionierten Kollegen zusammen. Erfahre hier mehr.

Social

Folge mir...

Copyright © 2025 · Digital Pro on Genesis Framework