Forum Discussion

Speedylux's avatar
Speedylux
Regular Visitor
2 years ago
Solved

generate table without duplicates and count grouped values only as one element

Hello everyone,

 

I'm new to PowerBi and would like to summarize certain data but struggle to understand how functions such as FILTER, CALCULATETABLE work together to make this possible. Until now I was not able to solve this.

 

a) I need to ensure that reported successive errors that are all equal to the previous are counted only as "one error". This selection should be shown in a different table. (see different colours within source data table in column error0 and error1 as example for the needed grouping. The same colour should be counted always as "one error.

 

b) How can the packageID be used as first filtering element before counting the number of grouped errors to avoid that the same error within two different packageIDs is counted only once. (Example at within the given data at index 4 to 5 column error2: Following my question a) the "E20" error would only get counted as "one error". In this particular case it is speparated due to the change of the packageID. What change in the DAX / M statements would be needed?

 

c) create a new table that summarizes the data based on the packageID and collects the first and last time it each packageID was recorded, same for the index of the original data

 

=> What is the benefit of creating an additional calculated table, doing such an operation as measure or within the transformation data view?

=> Is it ensured, no matter which method from above is used, that additional filter that are applied to the dashboard may still work although they focus on other data that is given in the source table?

 

Source data:

 

 

 

indexpackageIDinsert timet_diff_prevt_diff_succerror0error1error2
0cr9123401.12.2023 12:00:00.000000010E1E0 
1cr9123401.12.2023 12:00:10.000000107E1E2E3
2cr9123401.12.2023 12:00:17.000000713E3E2 
3cr9123401.12.2023 12:00:30.000000137E3E1 
4cr9123401.12.2023 12:00:37.00000071E3E1E20
5bq457801.12.2023 12:00:38.00000017E0E20E20
6bq457801.12.2023 12:00:45.000000715E1E2E2
7bq457801.12.2023 12:01:00.000000155E1E2 
8bq457801.12.2023 12:00:05.00000050E1E2 

 

 

Needed result data for a): Count occurance of grouped errors as "one error"

erroroccurance e0occurance e1occurance e2occurance e0+e1+e2
E01102
E12103
E20314
E31012
E200123

 

Needed result data for c): create table for all packageIDs withouth duplicates, Find earlist and last insert time and store each value in an individual column as well as start time and end time which result from the insert time colum of the source data column "insert time" of the first and last element that belongs to the packageID.

 

packageIDstart indexend indexstart timeend time
cr912340401.12.2023 12:00:00.00000001.12.2023 12:00:37.000000
bq45785801.12.2023 12:00:38.00000001.12.2023 12:00:05.000000

 

 

Would appreciate any help to learn and understand better how data can be filtered and how such queries work.

 

Cheers

Speedy

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi  Speedylux ,

     

    In calculating the data you provided, I noticed a small discrepancy that I would like to share with you, I found that in result A, if the same color is counted 1 time, then E2 is counted 2 times in occurance e1.

     

    Here are the steps you can follow:

    1. Create calculated column.

    Error0-1 =
    var _count=COUNTX(FILTER(ALL('Table'),'Table'[error0]=EARLIER('Table'[error0])&&'Table'[packageID]=EARLIER('Table'[packageID])),[index])
    var _minindex=
    MINX(    FILTER(ALL('Table'),'Table'[packageID]=EARLIER('Table'[packageID])&&'Table'[error0]=EARLIER('Table'[error0])),[index])
    var _if=
        IF(
            _count=1,_count,
    IF(
        _count>1&&'Table'[index]=_minindex,1,0)
    )
    return
    _if
    Error0-2 =
    SUMX(
        FILTER(ALL('Table'),
        'Table'[error0]=EARLIER('Table'[error0])&&[error0]<>BLANK()),[Error0-1])
    Error1-1 =
    var _count=COUNTX(FILTER(ALL('Table'),'Table'[error1]=EARLIER('Table'[error1])&&'Table'[packageID]=EARLIER('Table'[packageID])),[index])
    var _minindex=
    MINX(   FILTER(ALL('Table'),'Table'[packageID]=EARLIER('Table'[packageID])&&'Table'[error1]=EARLIER('Table'[error1])),[index])
    var _if=
        IF(
            _count=1,_count,
    IF(
        _count>1&&'Table'[index]=_minindex,1,0)
    )
    return
    _if
    Error1-2 =
    SUMX(
        FILTER(ALL('Table'),
        'Table'[error1]=EARLIER('Table'[error1])&&[error1]<>BLANK()),[Error1-1])
    Error2-1 =
    var _count=COUNTX(FILTER(ALL('Table'),'Table'[error2]=EARLIER('Table'[error2])&&'Table'[packageID]=EARLIER('Table'[packageID])),[index])
    var _minindex=
    MINX(    FILTER(ALL('Table'),'Table'[packageID]=EARLIER('Table'[packageID])&&'Table'[error2]=EARLIER('Table'[error2])),[index])
    var _if=
        IF(
            _count=1,_count,
    IF(
        _count>1&&'Table'[index]=_minindex,1,0)
    )
    return
    _if
    Error2-2 =
    SUMX(
        FILTER(ALL('Table'),
        'Table'[error2]=EARLIER('Table'[error2])&&[error2]<>BLANK()),[Error2-1])

    2. Enter data – create a table.

    Create calculated column

    occurance e0 =
    var _value=
    MAXX(
        FILTER(ALL('Table'),
        'Table'[error0]=EARLIER('result table'[error])),[Error0-2])
    return
    IF(
        _value=BLANK(),0,_value)
    
    occurance e1 =
    var _value=
    MAXX(
        FILTER(ALL('Table'),
        'Table'[error1]=EARLIER('result table'[error])),[Error1-2])
    return
    IF(
        _value=BLANK(),0,_value)
    
    occurance e2 =
    var _value=
    MAXX(
        FILTER(ALL('Table'),
        'Table'[error2]=EARLIER('result table'[error])),[Error2-2])
    return
    IF(
        _value=BLANK(),0,_value)
    occurance e0+e1+e2 =
    [occurance e0] + [occurance e1] + [occurance e2]

    3. Create calculated table.

    Table 2 =
    SUMMARIZE(
        'Table','Table'[packageID],
        "start index",MINX(FILTER(ALL('Table'),'Table'[packageID]=EARLIER('Table'[packageID])),[index]),
        "end index",MAXX(FILTER(ALL('Table'),'Table'[packageID]=EARLIER('Table'[packageID])),[index]),
        "start time",MINX(FILTER(ALL('Table'),'Table'[packageID]=EARLIER('Table'[packageID])),[insert time]),
        "end time",MAXX(FILTER(ALL('Table'),'Table'[packageID]=EARLIER('Table'[packageID])),[insert time]))

    4. Result:

     

    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

1 Reply

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi  Speedylux ,

     

    In calculating the data you provided, I noticed a small discrepancy that I would like to share with you, I found that in result A, if the same color is counted 1 time, then E2 is counted 2 times in occurance e1.

     

    Here are the steps you can follow:

    1. Create calculated column.

    Error0-1 =
    var _count=COUNTX(FILTER(ALL('Table'),'Table'[error0]=EARLIER('Table'[error0])&&'Table'[packageID]=EARLIER('Table'[packageID])),[index])
    var _minindex=
    MINX(    FILTER(ALL('Table'),'Table'[packageID]=EARLIER('Table'[packageID])&&'Table'[error0]=EARLIER('Table'[error0])),[index])
    var _if=
        IF(
            _count=1,_count,
    IF(
        _count>1&&'Table'[index]=_minindex,1,0)
    )
    return
    _if
    Error0-2 =
    SUMX(
        FILTER(ALL('Table'),
        'Table'[error0]=EARLIER('Table'[error0])&&[error0]<>BLANK()),[Error0-1])
    Error1-1 =
    var _count=COUNTX(FILTER(ALL('Table'),'Table'[error1]=EARLIER('Table'[error1])&&'Table'[packageID]=EARLIER('Table'[packageID])),[index])
    var _minindex=
    MINX(   FILTER(ALL('Table'),'Table'[packageID]=EARLIER('Table'[packageID])&&'Table'[error1]=EARLIER('Table'[error1])),[index])
    var _if=
        IF(
            _count=1,_count,
    IF(
        _count>1&&'Table'[index]=_minindex,1,0)
    )
    return
    _if
    Error1-2 =
    SUMX(
        FILTER(ALL('Table'),
        'Table'[error1]=EARLIER('Table'[error1])&&[error1]<>BLANK()),[Error1-1])
    Error2-1 =
    var _count=COUNTX(FILTER(ALL('Table'),'Table'[error2]=EARLIER('Table'[error2])&&'Table'[packageID]=EARLIER('Table'[packageID])),[index])
    var _minindex=
    MINX(    FILTER(ALL('Table'),'Table'[packageID]=EARLIER('Table'[packageID])&&'Table'[error2]=EARLIER('Table'[error2])),[index])
    var _if=
        IF(
            _count=1,_count,
    IF(
        _count>1&&'Table'[index]=_minindex,1,0)
    )
    return
    _if
    Error2-2 =
    SUMX(
        FILTER(ALL('Table'),
        'Table'[error2]=EARLIER('Table'[error2])&&[error2]<>BLANK()),[Error2-1])

    2. Enter data – create a table.

    Create calculated column

    occurance e0 =
    var _value=
    MAXX(
        FILTER(ALL('Table'),
        'Table'[error0]=EARLIER('result table'[error])),[Error0-2])
    return
    IF(
        _value=BLANK(),0,_value)
    
    occurance e1 =
    var _value=
    MAXX(
        FILTER(ALL('Table'),
        'Table'[error1]=EARLIER('result table'[error])),[Error1-2])
    return
    IF(
        _value=BLANK(),0,_value)
    
    occurance e2 =
    var _value=
    MAXX(
        FILTER(ALL('Table'),
        'Table'[error2]=EARLIER('result table'[error])),[Error2-2])
    return
    IF(
        _value=BLANK(),0,_value)
    occurance e0+e1+e2 =
    [occurance e0] + [occurance e1] + [occurance e2]

    3. Create calculated table.

    Table 2 =
    SUMMARIZE(
        'Table','Table'[packageID],
        "start index",MINX(FILTER(ALL('Table'),'Table'[packageID]=EARLIER('Table'[packageID])),[index]),
        "end index",MAXX(FILTER(ALL('Table'),'Table'[packageID]=EARLIER('Table'[packageID])),[index]),
        "start time",MINX(FILTER(ALL('Table'),'Table'[packageID]=EARLIER('Table'[packageID])),[insert time]),
        "end time",MAXX(FILTER(ALL('Table'),'Table'[packageID]=EARLIER('Table'[packageID])),[insert time]))

    4. Result:

     

    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