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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
Shalva
New Member

Getting Data bewteen range of dates

Hello

I have a table of workers with dates

NameStart Date in the companyEnd Date in the company
A15/03/2031/03/20
B01/03/2031/04/20
C10/03/2010/05/20

And so on...

I want to alculate and show the amount of wages that have been paid each month

 

NameMarchAprilMay
A0.500
B110
C0.610.3

 

I want to find the best and efficent way to do it because I have a lot of workers and can't make 12 rows for each month for each individual.

I tried to make a measure for each month but it didn't work

 

MeasureMarch=

var StartMonth = Max( Table[Start Date], Date(2020,03,01))

var EndMonth = Min( Date(2020,03,31), Table[End Date])

var Wage=Calculate((NextMonth-ThisMonth)/30)

FlagAnd( Month(StartMonth)=3, Month(EndMonth=3)), 

return If( Flag, Wage, 0)

 

Any thoughts?

1 ACCEPTED SOLUTION
CNENFRNL
Community Champion
Community Champion

@Shalva , both DAX and Power Query do the trick.

 

DAX solution

Screenshot 2021-02-19 021925.png

 

PQ solution,

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTI01Tcw1jcyADKNDaHMWJ1oJSeggIEhQs5A38AEJucM0mcAlwMxTcFysQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, #"Start Date in the company" = _t, #"End Date in the company" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Start Date in the company", type date}, {"End Date in the company", type date}}, "Fr"),
    #"Added Custom" = Table.AddColumn(
        #"Changed Type",
        "Custom",
        each List.Accumulate(
            List.Dates([Start Date in the company], Duration.Days([End Date in the company]-[Start Date in the company])+1,#duration(1,0,0,0)),
            [],
            (s,c) => let yyyyMM = Date.ToText(c, "yyyyMM"), value = Record.FieldOrDefault(s, yyyyMM, "NA") in if value = "NA" then Record.AddField(s, yyyyMM, 1) else s & Record.AddField([], yyyyMM, value+1)
        )
    ),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Start Date in the company", "End Date in the company"}),
    Custom1 = Table.TransformColumns(
        #"Removed Columns",
        {{"Custom",
            each List.Accumulate(
                Record.FieldNames(_),
                [],
                (s,c) => Record.AddField(s, c, Number.Round(Record.Field(_,c)/Date.DaysInMonth(Date.From(c&"01")),2))
            )
        }}
    ),
    #"Expanded Custom" = Table.ExpandRecordColumn(Custom1, "Custom", {"202003", "202004", "202005"}, {"202003", "202004", "202005"})
in
    #"Expanded Custom"

Screenshot 2021-02-19 022312.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

1 REPLY 1
CNENFRNL
Community Champion
Community Champion

@Shalva , both DAX and Power Query do the trick.

 

DAX solution

Screenshot 2021-02-19 021925.png

 

PQ solution,

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTI01Tcw1jcyADKNDaHMWJ1oJSeggIEhQs5A38AEJucM0mcAlwMxTcFysQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, #"Start Date in the company" = _t, #"End Date in the company" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Start Date in the company", type date}, {"End Date in the company", type date}}, "Fr"),
    #"Added Custom" = Table.AddColumn(
        #"Changed Type",
        "Custom",
        each List.Accumulate(
            List.Dates([Start Date in the company], Duration.Days([End Date in the company]-[Start Date in the company])+1,#duration(1,0,0,0)),
            [],
            (s,c) => let yyyyMM = Date.ToText(c, "yyyyMM"), value = Record.FieldOrDefault(s, yyyyMM, "NA") in if value = "NA" then Record.AddField(s, yyyyMM, 1) else s & Record.AddField([], yyyyMM, value+1)
        )
    ),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Start Date in the company", "End Date in the company"}),
    Custom1 = Table.TransformColumns(
        #"Removed Columns",
        {{"Custom",
            each List.Accumulate(
                Record.FieldNames(_),
                [],
                (s,c) => Record.AddField(s, c, Number.Round(Record.Field(_,c)/Date.DaysInMonth(Date.From(c&"01")),2))
            )
        }}
    ),
    #"Expanded Custom" = Table.ExpandRecordColumn(Custom1, "Custom", {"202003", "202004", "202005"}, {"202003", "202004", "202005"})
in
    #"Expanded Custom"

Screenshot 2021-02-19 022312.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.