Forum Discussion

KervBruce's avatar
KervBruce
Advocate I
2 years ago

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

  • 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?

    • KervBruce's avatar
      KervBruce
      Advocate I

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

      • MFelix's avatar
        MFelix
        Super User

        Hi KervBruce ,

         

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

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    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.

    • KervBruce's avatar
      KervBruce
      Advocate I

      Thanks Greg_Deckler 
      Here is my model: 

      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.