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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
AlB
Super User
Super User

Problems while building a table dynamically

Hi all,

Don't be put off by the length of the post, it's mostly because of the supporting material. This question is derived from this post by @AlexisOlson, although it is not really necessary to read it to answer this.

I want to create a table dynamically within a measure. The table is calculated from Table1 ( see below) and should look like this:

 

Expected resultExpected result

That is, we take the customer_IDs, calculate the product list that each has on Table1 and, as last column, we count how many times each product list comes up. In the example we see that A comes up twice and all the rest only once. I don't have problems to create the two first columns but it gets tricky when creating the third column, "Count". If I had a pre-existing table with the first two columns as:

 

ExistingTableExistingTable

I would just create the "Count" column easily with something like:

 

ExistingTable[Count] = CALCULATE(DISTINCTCOUNT(ExistingTable[customer_id]);ALL(ExistingTable[customer_id]))

Now I am trying to replicate that logic in my dynamically created table:

 

Measure =
VAR _AuxTable =
    ADDCOLUMNS (
        ADDCOLUMNS (
            ALL ( Table1[customer_id] );
            "ProdListCol"; CONCATENATEX (
                CALCULATETABLE ( VALUES ( Table1[product] ) );
                Table1[product];
                ", "
            )
        );
        "Count"; WHAT SHOULD I DO HERE to create the column??
    )
RETURN
    Whatever goes here is not relevant now  COUNTROWS ( _AuxTable )

 

 but the problem is that I cannot directly reference the table created in the inner ADDCOLUMNS and that the use of its columns in expressions is restricted. For instance,  COUNT([ProdListCol]) is not allowed. This limits the options in the code I could use to create the column. How would you solve this?  How would you create that column?

I am particularly interested in the example shown (i.e, with ADDCOLUMNS) and/or in using SUMMARIZECOLUMNS. Not that much in using SUMMARIZE, since @AlexisOlson showed how to do it in his post.

 

Many thanks in advance.

 

Table1:

 

customer_id

date_idproduct

1

9/11/2018A
110/11/2018A
110/11/2018B
111/11/2018C
111/11/2018A
29/11/2018C
210/11/2018D
211/11/2018E
211/11/2018A
310/11/2018A
310/11/2018B
311/11/2018A
311/11/2018B
311/11/2018B
410/11/2018A
411/11/2018A
59/11/2018A
510/11/2018B
510/11/2018E
510/11/2018D
511/11/2018C
511/11/2018A
69/11/2018A
610/11/2018A
611/11/2018A

 

 

 

 

   

 

2 ACCEPTED SOLUTIONS
AlexisOlson
Super User
Super User

You can do it using ADDCOLUMNS, but I would still use multiple steps.

 

Measure = 
VAR _InnerAux =
    ADDCOLUMNS (
        ALL ( Table1[customer_id] ),
        "ProdListCol", CONCATENATEX (
            CALCULATETABLE ( VALUES ( Table1[product] ) ),
            Table1[product],
            ", "
        )
    )
VAR _AuxTable =
    ADDCOLUMNS (
        _InnerAux,
        "Count", COUNTROWS ( FILTER ( _InnerAux, [ProdListCol] = EARLIER ( [ProdListCol] ) ) )
    )
RETURN
    COUNTROWS( _AuxTable )

View solution in original post

Another option would be to use a helper measure

 

MeasureProductList = CONCATENATEX(VALUES(Table1[product]),Table1[product],", ") 

 

 

inside of the table measure

 

 

Measure =
VAR _AuxTable =
    ADDCOLUMNS (
        ADDCOLUMNS (
ALL ( Table1[customer_id] ),
"ProdListCol", [MeasureProductList] ), "Count", COUNTROWS ( FILTER ( VALUES ( Table1[customer_id] ), [ProdListCol] = [MeasureProductList] ) ) ) RETURN COUNTROWS( _AuxTable )

 

If you really need everything in a single measure, then you can always just repeat the helper measure code within the second measure.

 

Measure =
VAR _AuxTable =
    ADDCOLUMNS (
        ADDCOLUMNS (
            ALL ( Table1[customer_id] ),
            "ProdListCol", CALCULATE ( CONCATENATEX ( VALUES ( Table1[product] ), Table1[product], ", " ) )
        ),
        "Count", COUNTROWS (
            FILTER (
                VALUES ( Table1[customer_id] ),
                [ProdListCol]
                    = CALCULATE ( CONCATENATEX ( VALUES ( Table1[product] ), Table1[product], ", " ) )
            )
        )
    )
RETURN
    COUNTROWS(_AuxTable)

 

View solution in original post

4 REPLIES 4
v-jiascu-msft
Employee
Employee

Hi @AlB,

 

Could you please mark the proper answers as solutions?

 

Best Regards,

Dale

Community Support Team _ Dale
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
AlexisOlson
Super User
Super User

You can do it using ADDCOLUMNS, but I would still use multiple steps.

 

Measure = 
VAR _InnerAux =
    ADDCOLUMNS (
        ALL ( Table1[customer_id] ),
        "ProdListCol", CONCATENATEX (
            CALCULATETABLE ( VALUES ( Table1[product] ) ),
            Table1[product],
            ", "
        )
    )
VAR _AuxTable =
    ADDCOLUMNS (
        _InnerAux,
        "Count", COUNTROWS ( FILTER ( _InnerAux, [ProdListCol] = EARLIER ( [ProdListCol] ) ) )
    )
RETURN
    COUNTROWS( _AuxTable )

Another option would be to use a helper measure

 

MeasureProductList = CONCATENATEX(VALUES(Table1[product]),Table1[product],", ") 

 

 

inside of the table measure

 

 

Measure =
VAR _AuxTable =
    ADDCOLUMNS (
        ADDCOLUMNS (
ALL ( Table1[customer_id] ),
"ProdListCol", [MeasureProductList] ), "Count", COUNTROWS ( FILTER ( VALUES ( Table1[customer_id] ), [ProdListCol] = [MeasureProductList] ) ) ) RETURN COUNTROWS( _AuxTable )

 

If you really need everything in a single measure, then you can always just repeat the helper measure code within the second measure.

 

Measure =
VAR _AuxTable =
    ADDCOLUMNS (
        ADDCOLUMNS (
            ALL ( Table1[customer_id] ),
            "ProdListCol", CALCULATE ( CONCATENATEX ( VALUES ( Table1[product] ), Table1[product], ", " ) )
        ),
        "Count", COUNTROWS (
            FILTER (
                VALUES ( Table1[customer_id] ),
                [ProdListCol]
                    = CALCULATE ( CONCATENATEX ( VALUES ( Table1[product] ), Table1[product], ", " ) )
            )
        )
    )
RETURN
    COUNTROWS(_AuxTable)

 

Greg_Deckler
Super User
Super User

Well, one way to do this, although I haven't gotten it down to a single measure would be this:

 

Table = 
VAR __table = 
        ADDCOLUMNS (
            ALL ( Table3[customer_id] ),
            "ProdListCol", CONCATENATEX (
                CALCULATETABLE ( VALUES ( Table3[product] ) ),
                Table3[product],
                ", "
            )
        )
RETURN __table


Measure 3 = 
VAR __prodlistcol = MAX('Table'[ProdListCol])
RETURN
COUNTX(FILTER(ALL('Table'),[ProdListCol] = __prodlistcol),[ProdListCol])


Table 2 = ADDCOLUMNS('Table',"__Count",[Measure 3])

With a little more work, may be able to get this into a single measure perhaps, not a trivial problem. Attached PBIX for reference.


@ 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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.