Query by Example to Extract YYYY-MM from a Date Column

I was answering a question for a student in one of my online training courses this week. The question was, “Do you know of a way in power query to efficiently extract YYYY-MM from a Date column?” This can be done ‘manually’ with multiple steps including:

  1. Extract the Year into 1 column
  2. Extract the Month Number into a second column.
  3. Deal with the need for a leading zero before the months 1 through 9
  4. Merge the columns together with a “-” as a separator

If you know how to write M code, you could manually write a single line that will do the step for you.  But I am a believer in using the UI to help you when ever possible – you should only have to resort to M code on rare occasions.

Let me show you how to do this using the Add Columns from Examples feature.

To get some sample data happening, I used a Calendar table that I had previously produced and stored in OneNote.  You can read my article showing how to create a calendar table using Power Query if you want to get this code.  The technique will work with any table that has a Date column.

These are the steps to follow:

  • Edit the query that contains the Date column in Power Query.
  • Select the Date column.
  • Select Add Column from the menu tab.
  • Select Query By Example\Query by Selection.

A dialog box appears prompting you to enter example values in the new column that you are adding.

  • Give an example for the first month in the first row (see below).

  • Next, give an example for the second month (see below).  It is important to note that this is not the second row in the table, it is row number 32.  The reason I chose this row is that doing so helps the wizard see the pattern in the data

The wizard then recognises the pattern and adds the rest of the values in the new column.  Note: at this point I should have renamed the column from “Custom” to something like “YYYY-MM” but I forgot to do that.  I changed it after closing the wizard.

  • Check the values for correctness and click on OK.

  • Now I renamed the column.  Note in the video, I did this by editing the line of code generated by the wizard rather than renaming the column itself.

  • Click Close & Apply.

The table is then loaded. You can now add the column YYYY-MM to a Matrix or Table and view the values.

I recorded the steps in the following video as it makes it easy to see how the end to end process works.

Want to Learn Power Query in a Structured Way?

My Power Query Online training course consists of 7.5 hours of online video training material that will teach you everything you need to know about using this fabulous tool for both Power BI and Excel. You can watch some free sample videos and find out more about registration following the link above.

9 thoughts on “Query by Example to Extract YYYY-MM from a Date Column”

  1. Thanks for sharing Matt!

    Will have a play with this later as i know I have a solution where this could be used instead of deriving Year & Month from a date and then using a concatenation with a ‘-‘.

    I’m also wondering if using this supports Query Folding.

  2. Thanks for sharing Andrew. Actually, I am a believer in learning through the UI. Use the UI to do the work and then look at the formulas and learn and hack away all you can. I think your observation is great and taking the time to do this will make you a better Power Query user.

  3. Looking at the code generated by the wizard, I had the idea to simplify the expression for the added column to Table.AddColumn(#”Renamed Columns2″, “YYYY-MM”, each Date.ToText([Date], “yyyy-MM”)).
    It seems like the format string in the Date.ToText() function allows any arbitrary combination of day month and year.

    Of course, that does mean knowing the functions and writing M code in the “Custom Column” step instead of letting the “Column from Examples” work it out for you, but then again, I just feel funny when I see the Date.ToText(0 function used twice and then the results combined.

    Nice tip.

  4. It’s 6 of one and half a dozen of the other. As a rule, I prefer to do it in Power Query to keep all the transformations together. The exception is if I need to use a measure in the column, then I would prefer DAX. But it is personal preference.

  5. Out of interest: what’s the benefit of doing this in Power Query compared with adding a calculated column and using FORMAT([Date],”yyyy-mm”)? TIA

  6. What a time saver that Query By Example is! I usually do this same exact formatting with an if statement based on the month number and then a series of concatenations [Year] && “-‘ && [month] && ……. .
    This has many more uses – thanks for sharing !

  7. Matthew Runyon

    Ooh, nice! That had never occurred to me, there’s all kinds of date format changes that could be done much more easily this way, thanks for showing this!

Leave a Comment

Your email address will not be published. Required fields are marked *