Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
metricwise
Regular Visitor

Create two columns together for MTD/YTD use case

Hello,

 

I am trying to create two columns - "Dates", "Tag" such that "Dates" contains all the dates that fall in MTD and then the corresponding rows to that should be tagged as "MTD".

 

I tried using Table.FromList however I don't know how to add create two columns.

When I use Table.FromRecords, I get this error - "We cannot convert the value #date(2020, 8, 1) to type Record"

 

#"Create Table" = Table.FromList(List.Dates(MonthStart, Duration.Days(Yesterday - MonthStart) + 1, #duration(1, 0, 0, 0)), Splitter.SplitByNothing(), {"Dates"})

 

Please suggest a solution.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i44FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),

    Today = DateTime.Date(DateTime.FixedLocalNow()),
    YearStart = Date.Day(#datetime(Date.Year(Today), 1, 1)),
    QuarterStart = Date.StartOfQuarter(Today),
    MonthStart = Date.StartOfMonth(Today),
    WeekStart = Date.StartOfWeek(Today, Day.Monday),
    Yesterday = Date.AddDays(Today, -1),

    #"Create Table" = Table.FromRecords(List.Dates(MonthStart, Duration.Days(Yesterday - MonthStart) + 1, #duration(1, 0, 0, 0))),
in
    #"Create Table"

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @metricwise ,

You could Add a Custom column using like this:

8.20 case5 follow.PNGThe full code in Advanced Editor:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("VdDLCcAwDAPQXXIuyHL+s4Tsv0ZTSrF6fMgG2WulDsLNLe1rpQEPNGRNiqIGKpomPVAwNJmBDFqIoHRwUEpM8GnB+U4aWIRnswYH2EIN7DIKjtA5euoFbtrN/x9xberfT/YN", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", type date}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type", "QTD", each if Date.IsInCurrentQuarter([Value]) then "QTD" else null),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "YTD", each if Date.IsInCurrentYear([Value]) then "YTD" else null),
    #"Added Custom" = Table.AddColumn(#"Added Custom2", "MTD", each if Date.IsInCurrentMonth([Value]) then "MTD" else null),
    #"Sorted Rows" = Table.Sort(#"Added Custom",{{"Value", Order.Descending}}),
    #"Reordered Columns" = Table.ReorderColumns(#"Sorted Rows",{"Value", "MTD", "QTD", "YTD"})
in
    #"Reordered Columns"

Then the final table will look like this:

8.20 case5 follow2.PNG

Best regards,

Eyelyn Qin

View solution in original post

8 REPLIES 8
Anonymous
Not applicable

Hi @metricwise ,

Did I answer your question? Please mark my reply as solution, thank you~

Anonymous
Not applicable

Hi @metricwise ,

According to my understanding ,you want to create Date column with a Tag column to specify the MTD/QTD/YTD ,right?

You could use the following formula:

DateTable =
CALENDAR ( "2019/11/1", TODAY () )
latestDateColumn =
CALCULATE ( MAX ( DateTable[Date] ), ALL ( DateTable ) )
monthDiff =
IF (
    DATEDIFF (
        SELECTEDVALUE ( DateTable[Date] ),
        SELECTEDVALUE ( DateTable[latestDateColumn] ),
        MONTH
    ) = 0,
    "MTD"
)
quarterDiff =
IF (
    DATEDIFF (
        SELECTEDVALUE ( DateTable[Date] ),
        SELECTEDVALUE ( DateTable[latestDateColumn] ),
        QUARTER
    ) = 0,
    "QTD"
)
yearDiff =
IF (
    DATEDIFF (
        SELECTEDVALUE ( DateTable[Date] ),
        SELECTEDVALUE ( DateTable[latestDateColumn] ),
        YEAR
    ) = 0,
    "YTD"
)
Tag =
IF (
    DATEDIFF (
        SELECTEDVALUE ( DateTable[Date] ),
        SELECTEDVALUE ( DateTable[latestDateColumn] ),
        MONTH
    ) = 0,
    "MTD",
    IF (
        DATEDIFF (
            SELECTEDVALUE ( DateTable[Date] ),
            SELECTEDVALUE ( DateTable[latestDateColumn] ),
            QUARTER
        ) = 0,
        "QTD",
        IF (
            DATEDIFF (
                SELECTEDVALUE ( DateTable[Date] ),
                SELECTEDVALUE ( DateTable[latestDateColumn] ),
                YEAR
            ) = 0,
            "YTD"
        )
    )
)

My visualization looks like this:

8.20.5.2.PNG

Is the result what you want? If you have any questions, please upload some data samples and expected output.

Please do mask sensitive data before uploading.

 

Best Regards,

Eyelyn Qin

HI @Anonymous ,

 

Thank you for your reply. I see that you have written a DAX Query for this however I want to implement it in M for faster execution.

Anonymous
Not applicable

Hi @metricwise ,

You could Add a Custom column using like this:

8.20 case5 follow.PNGThe full code in Advanced Editor:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("VdDLCcAwDAPQXXIuyHL+s4Tsv0ZTSrF6fMgG2WulDsLNLe1rpQEPNGRNiqIGKpomPVAwNJmBDFqIoHRwUEpM8GnB+U4aWIRnswYH2EIN7DIKjtA5euoFbtrN/x9xberfT/YN", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", type date}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type", "QTD", each if Date.IsInCurrentQuarter([Value]) then "QTD" else null),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "YTD", each if Date.IsInCurrentYear([Value]) then "YTD" else null),
    #"Added Custom" = Table.AddColumn(#"Added Custom2", "MTD", each if Date.IsInCurrentMonth([Value]) then "MTD" else null),
    #"Sorted Rows" = Table.Sort(#"Added Custom",{{"Value", Order.Descending}}),
    #"Reordered Columns" = Table.ReorderColumns(#"Sorted Rows",{"Value", "MTD", "QTD", "YTD"})
in
    #"Reordered Columns"

Then the final table will look like this:

8.20 case5 follow2.PNG

Best regards,

Eyelyn Qin

Ajinkya369
Resolver III
Resolver III

Hi @metricwise ,

I did some change in your m code to resolve the error which you are getting.

I replaced the function Table.FromRecord with Table.FromValue

Here is the output of your M code:

OutputOutput

 

Modified M-Code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i44FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),

    Today = DateTime.Date(DateTime.FixedLocalNow()),
    YearStart = Date.Day(#datetime(Date.Year(Today), 1, 1)),
    QuarterStart = Date.StartOfQuarter(Today),
    MonthStart = Date.StartOfMonth(Today),
    WeekStart = Date.StartOfWeek(Today, Day.Monday),
    Yesterday = Date.AddDays(Today, -1),

    #"Create Table" = Table.FromValue(List.Dates(MonthStart, Duration.Days(Yesterday - MonthStart) + 1, #duration(1, 0, 0, 0)))
in
    #"Create Table"

If your problem is solved then accept this reply as a solution.

 

Thank you

Ajinkya

Master Data Analysis - 

Ajinkya369
Resolver III
Resolver III

Hi @metricwise ,

 

I did some change in your m query to resolve the error you are getting.

I replaced the function Table.FromRecord with Table.FromValue.

 

Here is the output image  after performing the changes:

OutputOutput

 

Working M Code:

 

 

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i44FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),

    Today = DateTime.Date(DateTime.FixedLocalNow()),
    YearStart = Date.Day(#datetime(Date.Year(Today), 1, 1)),
    QuarterStart = Date.StartOfQuarter(Today),
    MonthStart = Date.StartOfMonth(Today),
    WeekStart = Date.StartOfWeek(Today, Day.Monday),
    Yesterday = Date.AddDays(Today, -1),

    #"Create Table" = Table.FromValue(List.Dates(MonthStart, Duration.Days(Yesterday - MonthStart) + 1, #duration(1, 0, 0, 0)))
in
    #"Create Table"

 

 

 

If your problem is solved then please accept this as a solution

 

Thank you

Ajinkya

Master Data Analysis

Hi @Ajinkya369 ,

 

Thanks for the quick fix. However, I was wondering how can I add another column Column2 "Tag" which will store value as "MTD" against each of these Column1 "Dates" values? 

So, the output would be like below:

DatesTag
8/1/2020MTD
8/2/2020MTD
8/3/2020MTD
8/4/2020MTD
8/5/2020MTD
8/6/2020MTD

 

After this, I would apply the same logic to generate YTD, QTD values.

amitchandak
Super User
Super User

@metricwise , there is code here to generate a calendar using M.

 

see if that can help

https://radacad.com/create-a-date-dimension-in-power-bi-in-4-steps-step-1-calendar-columns

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.