Forum Discussion
How can I assign Single value to a variable dynamically?
hi Anonymous , hnguy71
I tried both of your code but unfortunately it didn't work as expected. My table is doing comparision between sales data of 2025 and 2024 on month by month basis. The thing is both with my code and your code, it shows up blank whereever selected year in slicer has no transaction. For example, if there was no transaction in March, 2025, It comes up blank for that month transaction ( which is expected) but it also comes up blank for 2024, ( there is transaction in March 2024).
Please let me know if you guys need any more information to support me through this.
Hi aforapple_27 ,
I see what’s going wrong now thanks for the clarification.
The issue happens because when a month has no data in 2025, the context carries over and blocks the 2024 data from showing too.
I’ve updated the measure to fix this by ensuring the 2024 data still appears even if 2025 is blank:
Amount for Previous Year =
VAR prevYear = SELECTEDVALUE('Raw data'[Year]) - 1
RETURN
CALCULATE(
SUM('Raw data'[Amount]),
TREATAS(
VALUES('Raw data'[Month]),
'Raw data'[Month]
),
'Raw data'[Year] = prevYear
)
I hope this will reslove your issue, if you need any further assistance, feel free to reach out.
If this post helps, then please give us Kudos and consider Accept it as a solution to help the other members find it more quickly.
Thankyou.
- Anonymous1 year agoNot applicable
Hi aforapple_27 ,
I wanted to check if you had the opportunity to review the information provided and also thank you Ashish_Mathur for your inputs. Please feel free to contact us if you have any further questions. If my response has addressed your query, please accept it as a solution and give a 'Kudos' so other members can easily find it.
Thank you.