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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Fantmas
Helper III
Helper III

Treat as function not working with variable

Hi Everyone,

I have the following DAX expression my issue is when I use this expression with le variable _Year is returns me a blank when I harcode the period I want example "2021.TOTAL", it is working and I do not understand what is the issue has I check the variable, if it is a text or a case issue but everything is align with what I need, any clue how to fix it ?

Act Previous Years = VAR default_year = YEAR(LASTDATE(ALL(DimDate[Date by Years])))-1

VAR selected_year = YEAR(SELECTEDVALUE('DimDate'[Date by Years]))-1

Var _Year = SWITCH(
    TRUE(),
    ISFILTERED(DimDate[Date by Years]),
    CONCATENATE(CONVERT(selected_year,STRING), ".TOTAL"),
    CONCATENATE(CONVERT(default_year,STRING), ".TOTAL")
)

VAR _Type = "ACTUAL - Actual"

	VAR _Result = CALCULATE(
		[Actual],
		TREATAS(
			{
				_Year
			},
			'Data'[Period Available]
		),
		TREATAS(
			{
				_Type
			},
			'Data'[Type]
		)
	)
	RETURN
		_Result

 

1 REPLY 1
amustafa
Solution Sage
Solution Sage

You can do some debugging by creating individual measures for each part of your expression (like for default_year, selected_year, and _Year) and use them in a table visualization to see their individual outputs. This can help you pinpoint where the problem lies.





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.