Please note javascript is required for full website functionality.

Blog

Power Query: Fixed Expression

16 June 2021

Welcome to our Power Query blog. This week, I look at the M function Expression.Constant().

I have different types of constant data in each column as follows:

I am going to use the following function to show how data like this is created in M code:

            Expression.Constant(value as any) as text

This returns the M source code representation of a constant value.

I will create a custom column on the ‘Add Column’ tab for each of my columns.

The results for Last Name are:

For text values, I get the original text in speech marks (“”). I would argue with the Microsoft definition, as the value returned is type Any, not Text. This process will be repeated if I apply Expression.Constant() to EC Last Name.

Now I have two speech marks around each quotation mark. Curious to see what happens next? Me too…

So, I am adding two, then four, then eight speech marks. That’s enough for that one, I think. On to Employee ID:

My numerical value is simply converted to type Any. Applying Expression.Constant() to EC Employee ID would just add speech marks and I don’t want to do that again!

The next column is Start Date:

This time, the value returned is the M code that would be used to create the date from the base values of year, month and day.

Similarly for Duration, I get the M code to create the duration from the number of days, hours, minutes and seconds.

Finally, I get a similar result for Time, where I have the code to create the time value from the number of hours, minutes and seconds.

Next time I’ll look at a related function Expression.Evaluate().

Come back next time for more ways to use Power Query!

Newsletter