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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Sum sales units between 2 dates for each item number

I'm still very green but I have the following columns thus far and I want to sum Sales Units between the "First Date of Sale" and "2 Week Date" (including the first and last date for the full 14 days) for each unique item number.

Item NumberSales DateSales UnitsFirst Date of Sale2 Week Date
255401/01/20701/01/2001/14/20
255401/10/20101/01/2001/14/20
255401/22/20301/01/2001/14/20
399602/01/20402/01/2002/14/20
788403/01/20303/01/2003/14/20
788403/06/202

03/01/20

03/14/20
399602/22/206

02/01/20

02/14/20

 

My end goal is to have it look like this:

Item Number

First Date of Sale

Sales First 2 Weeks
255401/01/208
399602/01/204
788403/01/205
1 ACCEPTED SOLUTION
dax
Community Support
Community Support

Hi @Anonymous , 

You also could try below measure to see whether  it work or not

 

 

Measure 2 = CALCULATE(SUM(t2[Sales Units]), FILTER(t2, t2[Sales Date]>=t2[First Date of Sale]&& t2[Sales Date]<=t2[First Date of Sale]+14))

 

 

636.PNG

If you want to use M code, you could try below M code

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jY5LCsAgDETvkrVQk/g9i+T+12hsqFZapCA4k/cW0xpQjAEceDz0kdeY16o/hh7FPW30hvGXTWSYNzbXmvqNBg5r1TjtXMqFeWBeq8ZPOxmmjT2X3LvTa4lFkRM=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Item Number" = _t, #"Sales Date" = _t, #"Sales Units" = _t, #"First Date of Sale" = _t, #"2 Week Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item Number", Int64.Type}, {"Sales Date", type date}, {"Sales Units", Int64.Type}, {"First Date of Sale", type date}, {"2 Week Date", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Sales Date]>=[First Date of Sale] and [Sales Date]<=[2 Week Date] then [Sales Units] else 0),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"Item Number"}, {{"sum", each List.Sum([Custom]), type number}, {"all", each _, type table [Item Number=number, Sales Date=date, Sales Units=number, First Date of Sale=date, 2 Week Date=date, Custom=number]}}),
    #"Expanded all" = Table.ExpandTableColumn(#"Grouped Rows", "all", {"Sales Date", "Sales Units", "First Date of Sale", "2 Week Date"}, {"Sales Date", "Sales Units", "First Date of Sale", "2 Week Date"})
in
    #"Expanded all"

or 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jY5LCsAgDETvkrVQk/g9i+T+12hsqFZapCA4k/cW0xpQjAEceDz0kdeY16o/hh7FPW30hvGXTWSYNzbXmvqNBg5r1TjtXMqFeWBeq8ZPOxmmjT2X3LvTa4lFkRM=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Item Number" = _t, #"Sales Date" = _t, #"Sales Units" = _t, #"First Date of Sale" = _t, #"2 Week Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item Number", Int64.Type}, {"Sales Date", type date}, {"Sales Units", Int64.Type}, {"First Date of Sale", type date}, {"2 Week Date", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Sales Date]>=[First Date of Sale] and [Sales Date]<=[2 Week Date] then [Sales Units] else 0),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"Item Number", "First Date of Sale"}, {{"sum", each List.Sum([Custom]), type number}})
in
    #"Grouped Rows"

 

Best Regards,
Zoe Zhi

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

4 REPLIES 4
dax
Community Support
Community Support

Hi @Anonymous , 

You also could try below measure to see whether  it work or not

 

 

Measure 2 = CALCULATE(SUM(t2[Sales Units]), FILTER(t2, t2[Sales Date]>=t2[First Date of Sale]&& t2[Sales Date]<=t2[First Date of Sale]+14))

 

 

636.PNG

If you want to use M code, you could try below M code

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jY5LCsAgDETvkrVQk/g9i+T+12hsqFZapCA4k/cW0xpQjAEceDz0kdeY16o/hh7FPW30hvGXTWSYNzbXmvqNBg5r1TjtXMqFeWBeq8ZPOxmmjT2X3LvTa4lFkRM=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Item Number" = _t, #"Sales Date" = _t, #"Sales Units" = _t, #"First Date of Sale" = _t, #"2 Week Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item Number", Int64.Type}, {"Sales Date", type date}, {"Sales Units", Int64.Type}, {"First Date of Sale", type date}, {"2 Week Date", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Sales Date]>=[First Date of Sale] and [Sales Date]<=[2 Week Date] then [Sales Units] else 0),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"Item Number"}, {{"sum", each List.Sum([Custom]), type number}, {"all", each _, type table [Item Number=number, Sales Date=date, Sales Units=number, First Date of Sale=date, 2 Week Date=date, Custom=number]}}),
    #"Expanded all" = Table.ExpandTableColumn(#"Grouped Rows", "all", {"Sales Date", "Sales Units", "First Date of Sale", "2 Week Date"}, {"Sales Date", "Sales Units", "First Date of Sale", "2 Week Date"})
in
    #"Expanded all"

or 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("jY5LCsAgDETvkrVQk/g9i+T+12hsqFZapCA4k/cW0xpQjAEceDz0kdeY16o/hh7FPW30hvGXTWSYNzbXmvqNBg5r1TjtXMqFeWBeq8ZPOxmmjT2X3LvTa4lFkRM=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Item Number" = _t, #"Sales Date" = _t, #"Sales Units" = _t, #"First Date of Sale" = _t, #"2 Week Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item Number", Int64.Type}, {"Sales Date", type date}, {"Sales Units", Int64.Type}, {"First Date of Sale", type date}, {"2 Week Date", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each if [Sales Date]>=[First Date of Sale] and [Sales Date]<=[2 Week Date] then [Sales Units] else 0),
    #"Grouped Rows" = Table.Group(#"Added Custom", {"Item Number", "First Date of Sale"}, {{"sum", each List.Sum([Custom]), type number}})
in
    #"Grouped Rows"

 

Best Regards,
Zoe Zhi

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

az38
Community Champion
Community Champion

Hi @Anonymous 

try create a measure

Measure = CALCULATE( SUM(Table[Sales Units]),
FILTER(ALL(Table), Table[Sales Date] >= SELECTEDVALUE(Table[First Date of Sale]) && Table[Sales Date] <= SELECTEDVALUE(Table[2 Week Date]) )
)

do not hesitate to give a kudo to useful posts and mark solutions as solution
LinkedIn
Anonymous
Not applicable

What if I want to do it in Power Query by adding a column? The reason is becasue right now the data I'm working with is 65 million rows and I want to condense it down to about 20K when it's all added together and then allow co-workers to use it in a pivot table in excel as that's all they're familiar with

Greg_Deckler
Community Champion
Community Champion

Sales First 2 Weeks = 

  VAR __FirstDate = MAX('Table'[First Date of Sale])

  VAR __LastDate = MAX('Table'[2 Week Date])

RETURN

  SUMX(FILTER('Table',[Sales Date] >= __FirstDate && [Sales Date] <= __LastDAte),[Sales Units])

 

Put that measure into a table visual along with Item Number (do not aggregate) and max first date of sale.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.