cancel
Showing results for
Did you mean:

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Regular Visitor

## Find Latest sale date per Product as of a specified date

hi - new to PowerBI. i have a data set of products and sales by date. I'd like to be able to set a date filter which returns the latest sale date for each product on or before the specified date.

For example, if i specifcy 3/8/2020, id like to be able to return the entire row for each product below on or before 3/8/2020. Essentially trying to add an IF statement to a Max or LastDate but cnat figure out how to do it.

 Product ProductID Sales Quantity Date A A1 150 5 3/5/2020 B B1 255 6 3/5/2020 C C1 200 4 3/5/2020 D D1 11 1 3/5/2020 E E1 300 7 3/5/2020 F F1 300 7 3/1/2020 A A1 150 5 3/4/2020 C C1 200 4 3/8/2020 E E1 300 7 3/10/2020
1 ACCEPTED SOLUTION
Community Support

Hi @Jkaplan88,

If you want to filter the product data which the date is on or before specific date, you can use relative date slicer to make filter:

In additional, you can create one measure to get the latest sale date of per product:

``mDate = CALCULATE(max('Products'[Date]),ALLEXCEPT('Products','Products'[ProductID]))``

Best Regards

Rena

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
9 REPLIES 9
Community Support

Hi @Jkaplan88 ,

Best Regards

Rena

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

Hi @Jkaplan88,

If you want to filter the product data which the date is on or before specific date, you can use relative date slicer to make filter:

In additional, you can create one measure to get the latest sale date of per product:

``mDate = CALCULATE(max('Products'[Date]),ALLEXCEPT('Products','Products'[ProductID]))``

Best Regards

Rena

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Super User

@Jkaplan88 , I created a disconnected Calendar table and this measure:

``````Measure =
VAR __Max = MAX('Calendar'[Date])
RETURN
IF(MAX('Table'[Date])<=__Max,1,0)``````

Used the measure as a filter. PBIX is attached.

Become an expert!: Enterprise DNA
External Tools: MSHGQM
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Regular Visitor

thanks Greg. Im looking to just pull the latest sale information on or before the specified date. For example, if just looking at product A, if i select 3/8 on the date range, then im still seeing sales on March 5 and MArch 4. Im trying just to find the latest sale on or before 3/8.

thank you

jordan

Super User

Did not get it completely. Can you share output need?

A measure like this can help you to get data on or before data

``````sales =
var _max = maxx(Table,Table[Date])
return
calculate(sum(table[Sales]),filter(all(Table),Table[Date]<=_max))``````

Regular Visitor

hi - sorry.  IF the user selected March 8, 2020 as the specified date, then the desired output table would be like this. Which returns the list of products and the latest respective sales data which occured on or before 3/8

 3/8/2020 Product ProductID Sales Quantity Date A A1 150 5 3/5/2020 B B1 255 6 3/5/2020 C C1 200 4 3/8/2020 D D1 11 1 3/5/2020 E E1 300 7 3/5/2020 F F1 300 7 3/1/2020
Super User

Updated PBIX to set Date to Latest to do the aggregation. Is this now correct?

Become an expert!: Enterprise DNA
External Tools: MSHGQM
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Regular Visitor

unfortunately not. seems like in your example, 'prduct E' is getting filtered out due ot the measure. looks like because there is a later sale date occuring after 3/8 therefore the 'latest date' aggregator isnt picking it up.

Super User

For both sales and quantity create

``````Sales =
var __maxdt =maxx(Table,table[Date])
VAR __id = MAX ( 'Table'[ProductID] )
VAR __date = CALCULATE ( MAX( 'Table'[Date] ), ALLSELECTED ( 'Table' ),  'Table'[ProductID] = __id )
RETURN CALCULATE ( sum ( 'Table'[Sales] ), VALUES ( 'Table'[ProductID] ), 'Table'[id] = __id, 'Table'[date] = __date ,'Table'[date] <= __maxdt )

Quantity =
var __maxdt =maxx(Table,table[Date])
VAR __id = MAX ( 'Table'[ProductID] )
VAR __date = CALCULATE ( MAX( 'Table'[Date] ), ALLSELECTED ( 'Table' ),  'Table'[ProductID] = __id )
RETURN CALCULATE ( sum ( 'Table'[Quantity] ), VALUES ( 'Table'[ProductID] ), 'Table'[id] = __id, 'Table'[date] = __date ,'Table'[date] <= __maxdt )
``````

Announcements

#### Fabric certifications survey

Certification feedback opportunity for the community.

#### Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

#### Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors