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

THE SELF-SERVICE-BI BLOG

Wir lieben Microsoft Power BI

  • Videokurse
    • Power Query: Dein umfassender Einstieg!
  • 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) / 12. März 2018

Writing documentation for custom M-functions, Part3

In my two previous posts you could see how to add documentation to custom M functions in Power Query for Excel and Power BI Desktop. In the current post I describe how such documentation can be added to the individual parameters of a function. I will also discuss how to make allowed values of a parameter selectable by dropdowns. Have fun.

You can download all code samples from here. If you already have my Power Query editor for Notepad++ (or want to build it) you can open the file in Notepad++. Otherwise open it in a simple text editor and paste it into the Advanced Editor of Power Query or Power BI Desktop. I will refer more frequently to the Microsoft Power Query for Excel Formula Language Specification. You can download it here.

Extend the functionality – sort the list in ascending or descending order

Now I would like to extend the funtionality of my custom function in that way, that the user can choose, wether the returned list of dates will be sorted in an ascending or descending order. Therefore I do the following steps:

  • I add the optional parameter SO (short for sort order).
  • I also change the functions‘ code, so that the list will be sorted in ascending order, if SO is null, or „ascending“. If it is something different, the sort order will be descending.
  • I add SO also to the definition of my custom type fnType

All these changes are highligted in red in the following screenshot:

Adding an optional parameter SO for choosing the sort order of dates, Power Query, Power BI Desktop
Adding an optional parameter „SO“ for choosing the sort order of dates

The result is the following:

The optional parameter "SO" in action, Power Query, Power BI Desktop
The optional parameter „SO“ in action

With this additional parameter I am now able to sort the date list in ascending or descending order by typing the words „descending“ or „ascending“ into the text box. A much better user experience would of course be if this were not a free field, but sorting could be selected via a dropdown. Let’s do this in the next step.

Limiting parameters to Allowed Values

Passing values of parameters as free text can often lead to errors. It would be better to use a dropdown, which lets you select the allowed contents of the parameters. This can be achieved via Documentation.AllowedValues as part of the metadata record of the specific parameter.

Adding allowed values to parameter SO, Power Query, Power BI Desktop
Adding allowed values to parameter SO

What has a been a usual textbox before, now is a dropdown, with only the allowed values (and an empty value) to choose for the user.

Select the sort direction from the drop-down list, Power Query, Power BI Desktop
Select the sort direction from the drop-down list

These allowed values are static. They won’t change over time. But what if I need the allowed values to be dynamical?

Dynamically control Allowed Values of the parameters

Now let’s say I want to dynamically set the values to choose. What if I want the user to be able to choose only date values from the future?

Define date values dynamically for the future, Power Query, Power BI Desktop
Define date values dynamically for the future

What I do in the above script is the following:

  • I define the variable Tomorrow as todays date (DateTime.Date(DateTime.LocalNow())) plus 1 day ( + #duration(1,0,0,0))
  • In addition I define the allowed values for both, date1 and date2 as a dynamic list of 100 dates, starting from tomorrow, by using List.Dates(Tomorrow, 100, #duration(1,0,0,0))

The resulting documentation looks like this:

Selecting dates from a drop down list of allowed dates, Power Query, Power BI Desktop
Selecting dates from a drop-down list of allowed dates

As you can see in the screenshot above, the parameter date1 (and also date2, even it is not visible on the screenshot) got a different user interface. While I had a small calendar icon first, to choose a specific date, I now have a dropdown list with only the allowed values in it. See the difference here:

Different user interface for dates and dates with allowed values only, Power Query, Power BI Desktop
Different user interface for dates and dates with allowed values only

Adding more valuable information to the function parameters

In addition to Documentation.AllowedValues, there is some other information that can be added to the documentation of a custom functions parameters. Those are the following:

  • Documentation.FieldCaption:
  • Documentation.FieldDescription
  • Documentation.SampleValues

The following script uses the above mentioned fields within the metadata record:

Adding more description fields to the parameters of the custom M function, Power Query, Power BI Desktop
Adding more description fields to the parameters of the custom M function

The expectation is that these added fields in the metadata record will lead to more information in the documentation. However, a look at the documentation shows that this is unfortunately not the case:

Newly added description fields doen't appear in the user interface, Power Query, Power BI Desktop
Newly added description fields don’t appear in the user interface

Hmm, that wasn’t what I expected. I have only found one way to make the parameter documentation visible anyway: By removing the documentation of the function (not the parameter), which is the red marked part in the following screenshot:

Removing the functions documentation to see the documentation of the parameters, Power Query, Power BI Desktop
Removing the functions documentation to see the documentation of the parameters

By removing all the red marked part of the M script, the documentation for the parameters becomes visible (but of course the documentation of the function disappears):

This is how parameter description looks like, Power Query, Power BI Desktop
This is how parameter description looks like

For me it looks as if I always have to choose between the documentation of my function or the documentation of my function parameters. Both at the same time seems to be impossible, which of course makes little sense. But just because I couldn’t do it doesn’t mean that it can’t be done. If anyone out there has an idea of how it works, please say 🙂

My thanks go to the authors of the following excellent contributions on this topic: Chris Webb, Matt Masson, Imke Feldmann:

  • https://github.com/Microsoft/DataConnectors/blob/master/docs/function-docs.md
  • https://blog.crossjoin.co.uk/2014/11/27/specifying-allowed-values-sample-values-and-descriptions-for-function-parameters-in-power-querypart-1
  • https://blog.crossjoin.co.uk/2014/12/02/specifying-allowed-values-sample-values-and-descriptions-for-function-parameters-in-power-querypart-2
  • http://www.thebiccountant.com/2017/05/11/how-to-edit-m-function-metadata/

Regards from Germany,

Lars

I write my posts for you, the reader. Please take a minute to help me write my posts as well as possible. Thank you 🙂

[yasr_visitor_multiset setid=2]

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: allowed values, Custom Functions, documentation, M, Power BI Desktop, Power Query

Leser-Interaktionen

Kommentare

  1. C. Marpert meint

    23. Dezember 2021 um 7:10 am

    Great content! There was a lot to take from here! Especially in combination with the custom connectors serious I was able to get my own function library working. Am looking forward to get custom connectors for Excel, altough I read it’s not very likely….
    Anyway thanks a lot!

  2. Leif Lange meint

    3. Mai 2021 um 1:29 am

    Sigh… i wish this commentary box would have a preview feature (or a delete function). Last attempt now:

    You can use <br> or </p> in your description or longdescription to achieve line breaks.

  3. Gokul Kannan meint

    23. Dezember 2020 um 7:59 am

    I got so much information from this post. Thank you for posting this content.

  4. PC meint

    27. August 2020 um 4:39 pm

    This is an unbelievably helpful series of articles and has been so helpful in adding polish to various functions I have created. Thanks for sharing your knowledge in a clear and concise way.

    • Lars Schreiber meint

      27. August 2020 um 7:51 pm

      Hey Paul,

      this series was a huge amount of work and I am really happy, that it provides value to you. Thanks for you feedback 🙂

      Cheers,
      Lars

  5. James meint

    3. Mai 2020 um 4:57 pm

    Excellent! Most useful.

  6. Martin East meint

    6. Februar 2020 um 4:58 pm

    Hi Lars, have you found a way to get Documentation.LongDescription to display with newlines? I tried adding #(lf) into the text, but the newlines are simply ignored. For example, I wanted to write something like:

    This function returns:
    false if blah blah
    true if blah blah
    null if blah blah
    error if blah blah

    But the newlines are simply ignored.

    Thanks for your advice. Alaaf !

    • Lars Schreiber meint

      11. Februar 2020 um 7:58 am

      Hi Martin,

      usually Character.Fromnumber(10) does the trick. If you e. g. write = "long text" & Character.FromNumber(10) & "even longer" you get two lines in return. But this doesn’t seem to work in the longdescription of the metadata record. Maybe special characters are not interpreted in this area? I am not sure.

      Cheers,
      Lars

      • Leif Lange meint

        3. Mai 2021 um 1:22 am

        Hi all,

        I found that both description, and longdescription fields support some HTML tags. So you can use , or in your text for line breaks, as well as a few other tags to format your text.

        Cheers,
        Leif

        • Leif Lange meint

          3. Mai 2021 um 1:25 am

          Sorry, obviously my reply above did not escape the HTML tags properly. I hope this will work now:

          You can use <br&gt or </p&gt in your description or longdescription to achieve line breaks.

  7. Dave Brann meint

    24. Juni 2019 um 12:40 pm

    Display of Documentation.FieldDescription is a bug that been accepted by Microsoft (sort of). See: https://community.powerbi.com/t5/Issues/Documentation-Description/idi-p/339171
    and
    https://github.com/Microsoft/DataConnectors/issues/72

    • Lars Schreiber meint

      25. Juni 2019 um 2:16 pm

      Thanks for the info, Dave. Have a good one 🙂

  8. Christopher Hastings meint

    24. Mai 2019 um 6:40 pm

    Dear Lars,
    I found your three tutorials on documenting M functions to be very helpful. Thank you so much for teaching us all! It is too bad that you cannot document both parameters and the main function. Maybe an idea for a future Power BI update? –Christopher Hastings

    P.S. The star ratings at the bottom of the page are not working right now or I would give it a 6 star!

    • Lars Schreiber meint

      27. Mai 2019 um 12:10 pm

      Hi Chris,

      thanks for your feedback and good to know, that my posts helped you out 🙂

      Cheers from Germany,
      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