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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
YesP
Helper I
Helper I

Create a Conditional Measure working with a Direct Query Semantic Model

Hi,

I am working with a Semantic Model so I am not able to use the Transform Data option from Power BI.

In the Semantic Model I have a column Code and I want to create a column Code Desccription. This new column is based in the second letter of the Code: For instance, if Code is CAB, second letter is A then Code Description will be Administrative. I want to use this Code Description in a Filter to create visualization with this Code Description column. Please your help!

Thanks,

YesP_0-1733635963938.png

 

1 ACCEPTED SOLUTION

@danextian it is not allowing me the pull the Table column,

YesP_1-1733640143130.png

 

Thanks for trying.

View solution in original post

10 REPLIES 10
v-yangliu-msft
Community Support
Community Support

Thanks for the reply from danextian , please allow me to add some more information:
Hi  @YesP ,

 

Here are the steps you can follow:

1. In Power Query – Add Column – Custom Column.

if Text.Start([Code], 1)="C" and Text.Length([Code])=3
then 1 else 0

vyangliumsft_0-1733810783042.png

if [Custom]=1 and Text.Middle([Code], 1, 1)="A" then "Administrative"
else
if [Custom]=1 and Text.Middle([Code], 1, 1)="L" then "Liability"
else
if [Custom]=1 and Text.Middle([Code], 1, 1)="O" then "Operations"
else
if [Custom]=1 and Text.Middle([Code], 1, 1)="U" then "Utilities"
else null

2. Result:

vyangliumsft_1-1733810783045.png

 

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly

danextian
Super User
Super User

Hi @YesP 

Try this:

VAR SecondLetter =
    MID ( TableName[ColumnName], 2, 1 )
RETURN
    SWITCH (
        SecondLetter,
        "A", "Administrative",
        "L", "Liability",
        "O", "Operations",
        "U", "Utilities"
    )









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


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Hi @danextian , How I can add to this formule the following syntax:

If the column Code start with C and lenght=3 then secondLetter= "A","Administrative",.... the rest of the formule lines below. 

VAR SecondLetter =
    MID ( TableName[ColumnName], 2, 1 )
RETURN
    SWITCH (
        SecondLetter,
        "A", "Administrative",
        "L", "Liability",
        "O", "Operations",
        "U", "Utilities"
    )

  

@danextian It works! How I can use it in a filter? I try to use this new measure Description in a filter but it does not work. It just work in a table as long as I use the Code column as well.

I want the User be able to select the description in a filter and see all their transactions related to the description selected.

 

YesP_0-1733642082337.png

 

Please your help.

Thanks,

Create a calculated column instead. Measures don't have row context and are evaluated based on the other dimensions added to a viz so it cannot be used as a dimension or in a slicer. The first formula without SELECTEDVALUE creates a calculated column.










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


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

@danextian Thank you but I tried the first formule without SelectedValue but it does not works because my Datasource is a DirectQuery Semantic model storage in a different software.I think that I can not create a calculate column in Power BI using a DirectQuery Semantic model as Data source. Please any other sugestion how I can add the Description Column in a filter?

If it doesn't work because of Direct Query limitation, you can try creating a column in the query editor using first by adding a column that extracts the second letter and another conditional column to return the description. I've tested the steps below using MS SQL and they didn't break query folding. If this doesn't work, I would suggest to import the code column instead of DQ or create a view in SQL instead.

danextian_1-1733650694208.png

danextian_0-1733650652677.png

 

 










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


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

@danextian it is not allowing me the pull the Table column,

YesP_1-1733640143130.png

 

Thanks for trying.

Hi @YesP 

It appears you are creating a measure and not a calculated column. You cannot directly access a column in a measure (unless in a table expression or when filtering) without aggregating it beforehand. Use this:

 MID ( SELECTEDVALUE ( TableName[ColumnName] ), 2, 1 )









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


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Hi @danextian , This formule works great! you are awesome!  I want to add a condition, if a Code start with C and lenght =3 then 

VAR SecondLetter =
    MID(SELECTEDVALUE('Code'[Code ID]), 2, 1)
RETURN
    SWITCH (
      SecondLetter,
      "A","Administrative",
      "L", "Liabilities"
 
* Also, could you include Else (when the Code field is "blank","Unidentified"
Thank you so much,

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.