Search code examples
powerbidaxpowerquerypowerbi-desktopm

Convert Text with date column to display Month and Year in Power BI


I am just getting my hands on PowerBI, i am trying to get the date column which is in text to display Month and Year and i am not able to. Any directions. The column type is text which has value like this "2010-09-09T14:46:41+0000" i am trying to display "2010 September".


Solution

  • Tested the below in Power Query for Excel, but hopefully it should still work for you in Power BI. You could create a simple function like:

    ParseYearAndMonth = (someText as text) as text => DateTimeZone.ToText(DateTimeZone.From(someText), "yyyy MMMM")
    

    (In the function above, we convert the text to type DateTimeZone, and then just the year and month components of the DateTimeZone back to text. However, there is no error handling implemented.)

    Below is an example of invoking/using the function.

    let
        someText = "2010-09-09T14:46:41+0000",
        ParseYearAndMonth = (textToParse as text) as text => DateTimeZone.ToText(DateTimeZone.From(textToParse), "yyyy MMMM"),
        someAttempt = ParseYearAndMonth(someText)
    in
        someAttempt
    

    Edit:

    Your query might look something like this (although I can't be sure without testing):

    let
        Source = Facebook.Graph("graph.facebook.com/v2.8/Me/posts"),
        #"Removed Other Columns" = Table.SelectColumns(Source,{"message", "created_time", "id", "story"}),
        ParseYearAndMonth = (someText as text) as text => DateTimeZone.ToText(DateTimeZone.From(someText), "yyyy MMMM"),
        invokedFunction = Table.AddColumn(#"Removed Other Columns", "yearAndMonth", each ParseYearAndMonth([created_time]), type text)
    in
        invokedFunction