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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
abimfol1
Frequent Visitor

Highest previous value

I want a dax code to return the highest value of previous sales on a monthly basis. E.g see below 

 

MonthSales This monthHighest ever sales
Jan1,0001,000
Feb01,000
March2,0002,000

 

In the above table, when in January sales this month is 1000 and the highest ever was 1,000 and in February our highest sales ever still remains 1000 despite selling nothing for that month. However, in March the figure changed because the march sales has beaten that of January. Please I need a DAX to return this. 

 

@amitchandak @VahidDM @AlexisOlson 

1 ACCEPTED SOLUTION
v-cazheng-msft
Community Support
Community Support

Hi @abimfol1 

 

Please try the following Measure.

higest previous order mth =

VAR midT =

    TOPN (

        1,

        FILTER (

            SUMMARIZE (

                ALL ( DimTable ),

                DimTable[Month],

                DimTable[MonthYearNumber],

                "val", SUMX ( DimTable, [#Orders] )

            ),

            DimTable[MonthYearNumber] <= MIN ( DimTable[MonthYearNumber] )

        ),

        [val], DESC

    )

return MAXX ( midT, [val] )

 

Then, the result should look like this.

vcazhengmsft_0-1640765203923.png

 

For more details, please refer to the attached pbix file.

 

Best Regards,

Community Support Team _ Caiyun

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

View solution in original post

11 REPLIES 11
v-cazheng-msft
Community Support
Community Support

Hi @abimfol1 

 

Please try the following Measure.

higest previous order mth =

VAR midT =

    TOPN (

        1,

        FILTER (

            SUMMARIZE (

                ALL ( DimTable ),

                DimTable[Month],

                DimTable[MonthYearNumber],

                "val", SUMX ( DimTable, [#Orders] )

            ),

            DimTable[MonthYearNumber] <= MIN ( DimTable[MonthYearNumber] )

        ),

        [val], DESC

    )

return MAXX ( midT, [val] )

 

Then, the result should look like this.

vcazhengmsft_0-1640765203923.png

 

For more details, please refer to the attached pbix file.

 

Best Regards,

Community Support Team _ Caiyun

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly. If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

v-cazheng-msft
Community Support
Community Support

Hi @abimfol1 

 

May I know whether your issue has been resolved? If you still have problem on it, could you please show me your #Orders Measure and let me know how many tables in your model and the relationships among them? Thanks in advance!

 

Best Regards,

Community Support Team _ Caiyun

 

If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly. If you still have problems on it, please feel free to let us know. Thanks a lot!

Thanks for your response. this is my my order measure 

 

#Orders =
DISTINCTCOUNT('tbl_orders'[order_id]) + 0
 
and the only relationship is with the dimDate table on order_Date and date 
harshnathani
Community Champion
Community Champion

Hi @abimfol1 ,

 

Is Sales a measure or values in your table.

Also, can you share sample data and data model ( Which table does the store value come from).

 

Regards,

Harsh Nathani

Its a measure . it is a distinct count of order number. Problem is i kind of have a dax code that works but any months that the order count is zero then it doesnt return corresponding value of the highest previous month 

smpa01
Super User
Super User

@abimfol1  you can either create a calculated column like this

 

Column = 
var _monthIndex = Calculate(MAX('Table'[MonthIndex]))
return MAXX(FILTER(ALL('Table'),'Table'[MonthIndex]<=_monthIndex),'Table'[Sales])

 

smpa01_0-1640103997083.png

 

or a measure like this

Measure = 
var _monthIndex = MAX('Table'[MonthIndex])
return MAXX(FILTER(ALL('Table'),'Table'[MonthIndex]<=_monthIndex),'Table'[Sales])

 

smpa01_1-1640104052334.png

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

thanks for your response. it didnt work for me. when i drag in each store

abimfol1_0-1640105233092.png

 

Whenever i have 0 as a value for a month for each customer then i dont get the highest previous month using my own solution. However if the currenth month sales is greater than zero then it works . See below 

 

abimfol1_1-1640105345511.png

 

@abimfol1  any chance you can post the sample pbix?

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

CALCULATE(
[#Orders],
TOPN(
1,
FILTER(
SUMMARIZE(
ALL(dimDate),
dimDate[Month], dimDate[MonthYearNumber]
),
dimDate[MonthYearNumber] <= MIN(dimDate[MonthYearNumber])
), [#Orders], DESC
)
)
 
 
 
The above is the dax code i am currently using but doesnt work if the measure #orders returns zero for the current month like in the previously shared screenshot

@abimfol1  provide sample pbix or put the screenshot of the error here.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors