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
Anonymous
Not applicable

Extract a table information with disctinccount with date logic and category groupping

Hi, 

 

Could someone help solve this problem please?

Example fTable

IDStoreCategoryDate OpeningDate Closing
11A05/08/201910/08/2019
22A05/09/201911/09/2019
32B10/09/201911/09/2019
44C20/09/201925/09/2019
55D04/10/201905/10/2019
67D03/11/201904/12/2019
77E04/12/201907/12/2019
88E01/02/202008/01/2020
99E02/01/202003/01/2020

 

Then I tried two types of logic to extract distinct values of fTable[Classification]; by day (between opening and closing) and store. 
I created a table to visualize easily

 

 

Extract Table = 
ADDCOLUMNS(
    SUMMARIZE(
        fTable;
        dCalendar[Date];
        dStores[StoreID] //contains the store list
    );
    "CategoryCount"; 
    CALCULATE(
        DISTINCTCOUNT(fTable[Category]);
            FILTER(
                ALL(fTable);
                    fTable[Date Opening] >= MIN(dCalendar[Date])
                    && fTable[Date Closing] < MAX(dCalendar[Date]));
            FILTER(VALUES(fTable[Store]);fTable[Store]=dStores[StoreID]
            )
    )
)

 

 

the problem of Extract Table is for each StoreID it repeats the max of distinct value founded.

 

StoreIDDateCategoryCount
130/12/20192
131/12/20192
101/01/20202
102/01/20202
103/01/20202
230/12/20191
231/12/20191
201/01/20201
202/01/20201
203/01/20201
430/12/20191
431/12/20191
401/01/20201
402/01/20201
403/01/20201

 

After having no sucess on that I tried another way to Extract

 

 

Extract Table = 
ADDCOLUMNS(
    SUMMARIZE(
        fTable;
        dCalendar[Date];
        dStores[StoreID] //contains the store list
    );
    "CategoryCount"; 
     CALCULATE( DISTINCTCOUNT(fTable[Category]);
            FILTER( VALUES(fTable[Date Opening]); 
                           fTable[Date Opening]<=MAX(dCalendar[Date]));
            FILTER( VALUES(fTable[Date Closing]); 
                            fTable[Date Closing]>MIN(dCalendar[Date]));
            FILTER( VALUES(fTable[StoreID]); fTable[Store]=dStore[StoreID]) //Tables are related by StoreID
    )
)

 

 

The output of this table measure. The problem is that it only takes de opening date in consideration.

StoreIDDateCategoryCount
130/12/20190
131/12/20191
101/01/20201
102/01/20200
103/01/20200
230/12/20190
231/12/20191
201/01/20201
202/01/20200
203/01/20200
430/12/20190
431/12/20190
401/01/20200
402/01/20201
403/01/20200



Ideal output should be like this, considering the quantity of distinct count of categories each store has already opened on that exacly day

StoreIDDateCategoryCount
130/12/20190
131/12/20191
101/01/20202
102/01/20202
103/01/20200
230/12/20190
231/12/20191
201/01/20201
202/01/20201
203/01/20200
430/12/20190
431/12/20190
401/01/20200
402/01/20201
403/01/20200


In this case I need to make an average measure by day and store, thats why i'm trying to create a table. If there is a measure that execute that it would solve my problem too. 

Thanks in advance

3 REPLIES 3
v-lili6-msft
Community Support
Community Support

hi @Anonymous 

For your case, I think you have created a relationship between fTable table and dCalendar table by opening date field.

So it only takes de opening date in consideration.

You may try this way as below:

For the start/end date problem, you need a separate date table.

Then create a table by this logic:

Extract Table2 = 
ADDCOLUMNS (
    GENERATE(dStores,dCalendar),
    "CategoryCount", CALCULATE (
        DISTINCTCOUNT ( fTable[Category] ),
        FILTER (
            fTable ,
            fTable[Date Opening] <=  dCalendar[Date] 
                && fTable[Date Closing] >  dCalendar[Date] 
        ),
        FILTER ( VALUES ( fTable[Store] ), fTable[Store] = dStores[StoreID] )
    )
)

and here is similar post, you could refer to it.

 

If not your case, please share a simple sample pbix file and your expected output,I could not find the logic from above sample data and expected output

 

Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

@v-lili6-msft Thank you! The solution gives right result as expected with small dataset.


But now I'm facing another issue that is bad performance. Because dStores has more than 600 rows, dCalendar has 3 entire years and fTable has more than 20 thousant records, causing a pretty long delay. 

Is there a easy way to avoid this issue? Or that's an intrinsic problem of creating a table. 

 

 

hi @Anonymous 

Yes, It will go through and calculate two tables, for big dataset, it will be bad performance.

maybe we could use a measure insead.

Please share a sample pbix file and your expected output with the main columns you want to display.

 

Regards,

Lin

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

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.