Power BI is turning 10! Tune in for a special live episode on July 24 with behind-the-scenes stories, product evolution highlights, and a sneak peek at what’s in store for the future.
Save the dateEnhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.
Solved! Go to Solution.
Oh, Do you want to add a calculated column?
You can use my codes as a Measure, not a new Column.
If you want a column, use this code:
Time in Years =
Var _Len = LEN(Base[Time] )-4
RETURN
IF(
CONTAINSSTRING(Base[Time],"Q") , RIGHT(Base[Time],_Len)/4,
IF(
CONTAINSSTRING(Base[Time],"Y") , RIGHT(Base[Time],_Len)/1
)
)
If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
@YusufHabib
Not clear about the expected results but can you try this
Time in Years =
IF(
CONTAINSSTRING(Base[Time],"Q") , RIGHT(Base[Time],1)/4,
IF(
CONTAINSSTRING(Base[Time],"Y") , RIGHT(Base[Time],1)/1
)
)
⭕ Subscribe and learn Power BI from these videos
⚪ Website ⚪ LinkedIn ⚪ PBI User Group
Thanks for the quick answer but the query doesn't work for PD_Q10 or PD_Y10. Is there a way to make it work in those cases?
@YusufHabib
I posted my solution as well, this will accommodate any size of the text before Q or Y:
Time in Years =
VAR __LEN = LEN(Base[Time])
VAR __LOC_Q = SEARCH("Q",Base[Time],,0)
VAR __LOC_y = SEARCH("Y",Base[Time],,0)
RETURN
IF( __LOC_Q > 0 , RIGHT(Base[Time],__LEN - __LOC_Q)/4,
IF( __LOC_y > 0, RIGHT(Base[Time],__LEN - __LOC_y)/1
)
)
⭕ Subscribe and learn Power BI from these videos
⚪ Website ⚪ LinkedIn ⚪ PBI User Group
Hi @YusufHabib
Try this measure:
Time in Years =
VAR _2R =
RIGHT ( MAX ( Base[Time] ), 2 )
VAR _Y_Q =
LEFT ( _2R, 1 )
VAR _N =
RIGHT ( _2R, 1 )
RETURN
IF ( _Y_Q = "Q", _N / 4, _N )
Output:
If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Thanks for the quick answer but the query doesn't work for PD_Q10 or PD_Y10. Is there a way to make it work in those cases?
Hi @YusufHabib
Try this:
Time in Years =
Var _Len = LEN(MAX ( Base[Time] ))-4
VAR _2R =
RIGHT ( MAX ( Base[Time] ), _Len+1 )
VAR _Y_Q =
LEFT ( _2R, 1 )
VAR _N =
RIGHT ( _2R, _Len)
RETURN
IF ( _Y_Q = "Q", _N / 4, _N )
Output:
If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Really appreciate your prompt replies.
I pasted the code and recieved error 'Expressions that yield variant data-type cannot be used to define calculated columns'.
Oh, Do you want to add a calculated column?
You can use my codes as a Measure, not a new Column.
If you want a column, use this code:
Time in Years =
Var _Len = LEN(Base[Time] )-4
RETURN
IF(
CONTAINSSTRING(Base[Time],"Q") , RIGHT(Base[Time],_Len)/4,
IF(
CONTAINSSTRING(Base[Time],"Y") , RIGHT(Base[Time],_Len)/1
)
)
If this post helps, please consider accepting it as the solution to help the other members find it more quickly.
Appreciate your Kudos!!
Thanks! I didn't realize that columns and measures have differences in formula.
It works now.
Check out the July 2025 Power BI update to learn about new features.
This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.
User | Count |
---|---|
23 | |
10 | |
10 | |
9 | |
7 |