Code Generator | M (Power Query)

formatDate

This function, formatDate, takes a date value and formats it to 'DD-MM-YYYY' format. One can apply it in Power Query to format a list of dates effectively into the desired format.


Empty image or helper icon

Prompt

Takes a date value from a list passes to function

Answer

/*
Function: formatDate

Parameters:
dateValue (datetime): The date value to be formatted.

Return Type: text

Description: This function takes a date value and formats it to 'DD-MM-YYYY' format.
*/

let
    formatDate = (dateValue as datetime) as text =>
    
    let
        formattedDate = Text.Combine({Text.PadStart(Text.From(Date.Day(dateValue)), 2, "0"),
                                      "-",
                                      Text.PadStart(Text.From(Date.Month(dateValue)), 2, "0"),
                                      "-",
                                      Text.From(Date.Year(dateValue))}, "")
    in
        formattedDate
in
    formatDate

Usage Example:

Suppose you have a list with a date value, and you want to format each date in the list to 'DD-MM-YYYY' format. You can use the function formatDate as follows:

  1. Create a new query and load the list with date values.
  2. Add a Custom Column using the "Add Column" tab.
  3. Use the formatDate function in the Custom Column formula by referring to the date column.
  4. Close & Apply to see the formatted date column.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This function, formatDate, takes a date value and formats it to 'DD-MM-YYYY' format. One can apply it in Power Query to format a list of dates effectively into the desired format.