Skip to content

Instantly share code, notes, and snippets.

@ncalm
Created December 26, 2022 19:54
Show Gist options
  • Select an option

  • Save ncalm/ab577ccda71b1f47601f85c3b462319d to your computer and use it in GitHub Desktop.

Select an option

Save ncalm/ab577ccda71b1f47601f85c3b462319d to your computer and use it in GitHub Desktop.
M queries to inspect enumeration documentation
/*
A simple query to get the list of enumerations
*/
let
Source = Web.Page(Web.Contents("https://learn.microsoft.com/en-us/powerquery-m/enumerations")){0}[Data]
in
Source
/*
Query the #shared environment and return information about enumerations
*/
let
//Get all shared names in the current environment
Source = Record.ToTable(#shared),
/*
Filter to only include those names that end in .Type and are of type "type"
This includes all enums and other types such as type.any, type.text etc
*/
Filtered = Table.SelectRows(Source, each Text.EndsWith([Name],".Type") and Type.Is(Value.Type([Value]),type type)),
//Add a column containing a record of the Metadata attached to the type (this is the documentation)
MetaDataRecord = Table.AddColumn(Filtered, "MetaDataRecord", each Value.Metadata([Value])),
/*
Add a column showing the list of allowed values for the enumeration
If the metadata record doesn't have a field 'AllowedValues', then it isn't an enum
and this step shows an error in that row
*/
AllowedValues = Table.AddColumn(
MetaDataRecord,
"AllowedValues",
each Value.Metadata([Value])[Documentation.AllowedValues]
),
//Remove rows containing errors (i.e. rows that aren't enums)
Result = Table.RemoveRowsWithErrors(AllowedValues)
in
Result
/*
Query each of the web pages for enumeration documentation and return the list of allowed values
*/
let
//URLs for documentation begin with this text
URLPrefix = "https://learn.microsoft.com/en-us/powerquery-m/",
//The URL of the index page for enumerations
MainPage = URLPrefix & "enumerations",
//Get the table of enumerations from the index page
Source = Web.Page(Web.Contents(MainPage)){0}[Data],
//Create a table structure to load the allowed values into
EmptyTable = #table({"Name", "Value", "Description", "Enum", "URL"},{}),
/*
Iterate through the URLs, retrieve the table of allowed values and stack them on top of
each other along with the URL and the Enum name
*/
GetAllowedTables
= List.Accumulate(
Source[Name],
EmptyTable,
(output,enum) =>
let
URL = URLPrefix & Text.Replace(enum, ".", "-"),
WebTable = Web.Page(Web.Contents(URL)){0}[Data],
AddEnumName = Table.AddColumn(WebTable, "Enum", each enum),
AddEnumURL = Table.AddColumn(AddEnumName, "URL", each URL)
in
Table.Combine({output,AddEnumURL})
)
in
GetAllowedTables
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment