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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
KervBruce
Helper I
Helper I

Creating a Table in an IF statement

I want to create a table

I am trying to display a table of the tags selected in another table, but if none are selected I want to show an empty table, whereas the default behaviour is to display all of them.

Why doesn't this Table formula work?

Selected Tags =
VAR N = [Tags Selected]
 VAR BlankTable = DATATABLE("TagName",STRING,{{}})
 VAR TagTable = VALUES('Tags'[TagName])
 Return IF(N>0,TagTable,BlankTable)
The expression specified in the query is not a valid table expression.
I assume it doesn't like returning a table from an IF statement, but I can't see why?
6 REPLIES 6
MFelix
Super User
Super User

Hi @KervBruce 

 

You cannot use a Measure to create a Calculated table since the context is not recognized.

 

What do you want to achieve with this table?


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



Thanks, I thought that was propably the case.  See my reply to Greg on what I am trying to achieve.

Hi @KervBruce ,

 

Believe you have a great expert on the case so I will leave it to him. 😄


Regards

Miguel Félix


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

Proud to be a Super User!

Check out my blog: Power BI em Português



Greg_Deckler
Super User
Super User

@KervBruce Couple things. First, I believe you need this:

VAR N = COUNTROWS('Tags')

Unless [Tags Selected] is a measure that returns the number of tags selected.

 

Also, mixing measures with calculated tables and columns doesn't generally work as calculated tables and measures are not dynamic.

 

Finally, yes, DAX doesn't like it when you try to return different tables such as your IF statement. You could just return BLANK() instead of BlankTable and that will likely fix that issue.

 

But, overall, you will likely need to rethink your approach to this. Sorry, having trouble following, can you post sample data as text and expected output?
Not really enough information to go on, please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thanks @Greg_Deckler 
Here is my model: 

KervBruce_0-1712678327625.png

Yes I do have a measure called [Tags Selected] as follows:

Tags Selected = VAR TagsSelected = COUNTROWS(Tags)
RETURN IF(TagsSelected = [AllTags], 0, TagsSelected)
What I am trying to achieve is to filter a table of WorkItems based to tags slected, but I want to do this with AND rather than OR, so I have created the following measure to filter the table:
Selected Tags with AND-Only Condition =
var TheselectedTags=VALUES('Tag Assignments'[TagName])
var countRowsAss=
COUNTROWS(
    DISTINCT(
        SELECTCOLUMNS(
            FILTER(
                'Tag Assignments',
                RELATED(Tags[TagName]) in TheselectedTags
                ),
                "Tag",
                RELATED(Tags[TagName])
        )
    )
)
var countRowsTags=COUNTROWS(Tags)
return
IF([Tags Selected] = 0,0,
IF(countRowsAss>=countRowsTags,countRowsAss))
 
What I now want to display is the list of tags that are selected, but display none if none are slelected.
I did also try returning Blank() but that wasn't recognised as a table, hence constructing an empty table.

@KervBruce Well, I created an AND slicer once. You might be able to use it along with a Complex Selector. Really difficult without sample data to test with. 

Patient Cohort (AND Slicer) - Microsoft Fabric Community

The Complex Selector - Microsoft Fabric Community


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.