Forum Discussion

jamalq123's avatar
jamalq123
Advocate III
8 years ago
Solved

Date Table source changes

When  I started working in power bi, I simply made a link from excel to make the date table. Please find below the M Language which I took from advanced Editor

 

let
Source = Excel.Workbook(File.Contents("D:\Jamal Qamar DATA\Data\Geo\FPC Rating and Program wise revenue\rating cy17.xlsx"), null, true),
dDate_Sheet = Source{[Item="dDate",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(dDate_Sheet, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Date", type date}, {"Month", Int64.Type}, {"Month name", type text}, {"Year ", Int64.Type}, {"Month Year", type text}})
in
#"Changed Type"

 

Now I want to use Calendar option i.e. "dDate1 = CALENDAR(DATE(2015,1,1),MAX('Consolidated Data'[Day]))" inspite of the old method.

 

Please guide me to resolve the issue, In the old method I have to update the excel file time to time, whereas in the c aledar option this is very easy to use and not to update the table.

 

Regards,

 

Jamal Qamar

  • Hi Jamal,

     

    I found a way to replace the code directly.  You can check it out in this file.

    1. Create a function. (I found the M code here). 

    let CreateDateTable = (StartDate as date, EndDate as date, optional Culture as nullable text) as table =>
      let
        DayCount = Duration.Days(Duration.From(EndDate - StartDate)),
        Source = List.Dates(StartDate,DayCount,#duration(1,0,0,0)),
        TableFromList = Table.FromList(Source, Splitter.SplitByNothing()),    
        ChangedType = Table.TransformColumnTypes(TableFromList,{{"Column1", type date}}),
        RenamedColumns = Table.RenameColumns(ChangedType,{{"Column1", "Date"}}),
        InsertMonth = Table.AddColumn(RenamedColumns, "Month", each Date.Month([Date])),
        InsertMonthName = Table.AddColumn(InsertMonth, "Month Name", each Date.ToText([Date], "MMMM", Culture), type text),
        InsertYear = Table.AddColumn(InsertMonthName, "Year", each Date.Year([Date])),
        InsertQuarter = Table.AddColumn(InsertYear, "Month Year", each Date.QuarterOfYear([Date]))
           
      in
        InsertQuarter
    in
      CreateDateTable

    2. Replace the M code of the query dDate with the following one.

    let
        maxDate = List.Max(#"Consolidated Data"[Column1]), //Max date of another query, which makes this query dynamic.
        Source = Query(#date(2018, 2, 1), maxDate, null)
    in
        Source

    3. Apply the changes.

     

    Note: If the data you shared is a sample, you need to adjust the code to satisfy your data. The structure and the column names should be the same.

    Now you can get rid of the workbook file.

     

    Best Regards,

    Dale

     

11 Replies

  • v-jiascu-msft's avatar
    v-jiascu-msft
    Microsoft Employee

    Hi Jamal,

     

    I found a way to replace the code directly.  You can check it out in this file.

    1. Create a function. (I found the M code here). 

    let CreateDateTable = (StartDate as date, EndDate as date, optional Culture as nullable text) as table =>
      let
        DayCount = Duration.Days(Duration.From(EndDate - StartDate)),
        Source = List.Dates(StartDate,DayCount,#duration(1,0,0,0)),
        TableFromList = Table.FromList(Source, Splitter.SplitByNothing()),    
        ChangedType = Table.TransformColumnTypes(TableFromList,{{"Column1", type date}}),
        RenamedColumns = Table.RenameColumns(ChangedType,{{"Column1", "Date"}}),
        InsertMonth = Table.AddColumn(RenamedColumns, "Month", each Date.Month([Date])),
        InsertMonthName = Table.AddColumn(InsertMonth, "Month Name", each Date.ToText([Date], "MMMM", Culture), type text),
        InsertYear = Table.AddColumn(InsertMonthName, "Year", each Date.Year([Date])),
        InsertQuarter = Table.AddColumn(InsertYear, "Month Year", each Date.QuarterOfYear([Date]))
           
      in
        InsertQuarter
    in
      CreateDateTable

    2. Replace the M code of the query dDate with the following one.

    let
        maxDate = List.Max(#"Consolidated Data"[Column1]), //Max date of another query, which makes this query dynamic.
        Source = Query(#date(2018, 2, 1), maxDate, null)
    in
        Source

    3. Apply the changes.

     

    Note: If the data you shared is a sample, you need to adjust the code to satisfy your data. The structure and the column names should be the same.

    Now you can get rid of the workbook file.

     

    Best Regards,

    Dale

     

    • jamalq123's avatar
      jamalq123
      Advocate III

      Waoooooo Dale,

       

      Excellent task you did for me. Thanks a lot. Now my issue resolved. This is a relax time for me.

       

      Best Regards,

       

      Jamal Qamar

       

      • v-jiascu-msft's avatar
        v-jiascu-msft
        Microsoft Employee

        That's great! You are welcome, Jamal. Could you please mark my answer as solution?

         

        Best Regards,

        Dale

  • v-jiascu-msft's avatar
    v-jiascu-msft
    Microsoft Employee

    Hi Jamal,

     

    What's the issue? You know the DAX function Calendar and you can create a date table.

    Date_Table_source_changes

     

     

    Best Regards,

    Dale

    • jamalq123's avatar
      jamalq123
      Advocate III

      Hi Dale,

       

      Thanks for your response.

      Actually I want to replace the Date field of dDate table with the one which you suggested. Then I will connect the date from my sales table.

      As mentioned earlier, the date field is linked with Excel file and all the reports are based on this Date field. Every month I have to add month in excel table. 

       

      I want to use the Calendar function inspite of linkage with Excel. 

       

      Please download the pbix file from the link given below.

      https://drive.google.com/open?id=1WR_ghdl-wEWS-iQZzY9zTUeOuo32gXq7

       

      I shall be thankful to you that I you could use the Calendar function in Date field.

       

      Regards,

       

      Jamal Qamar

       

       

      • v-jiascu-msft's avatar
        v-jiascu-msft
        Microsoft Employee

        Hi Jamal,

         

        I'm afraid you have to replace the whole dDate table with a new date table. I hope I didn't miss anything. What could be the problem?

         

        Best Regards,

        Dale