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
Tamillllll
Frequent Visitor

count values in column A only when all corresponding column B values are same

A B
1893492 3740100990
1893492 3740100990
1893492 3740100990
1893492 3740100990
1893492 -1
1902593 3740100991
1902593 3740100991
1902593 3740100991
1902593 -1
1905279 -1
1905279 -1
1905279 -1
1905279 -1
1905279 -1
1905279 -1
1911152 -1
1911152 -1
1911152 -1
1911152 -1
1911152 -1
1911152 -1
1911152 -1


Hi,
I wanted to distinct count values in A only when all corresponding values are -1 in B.
eg: in above table, 1905279 and 1911152 ate two values which has all its corresponding B values as -1.
ths output is 2 for this.

Any help is appreciated.

Thanks,

7 REPLIES 7
talespin
Solution Sage
Solution Sage

@Tamillllll 

 

Apology, I did not check for all B values = -1

 

//Grouping data by A and B

UniqueCountA =
VAR groupTable =
                    SUMMARIZE(
                                TestTable,
                                TestTable[A],
                                TestTable[B]
                                )

//Adding column with count of A for each value of A
VAR countColA =
                ADDCOLUMNS(
                            groupTable, "CountA",
                            VAR _valA = [A]
                            RETURN CALCULATE(COUNTX( FILTER(groupTable, [A] = _valA), [A]))
)
 
//Checking for B = -1 and Count = 1, we want records only having rowcount of 1
VAR filterTable = FILTER(
                            countColA,
                            [B] = -1 && [CountA] = 1
)

 return COUNTX(filterTable, [A])
talespin
Solution Sage
Solution Sage

I am a newbie, It can be optimized further. Put it in a measure.
Just check, using Power Query might be a better solution, you have to compare performance.
 
 
//Grouping table by column A and B
Count =
VAR groupTable =
                    SUMMARIZE(
                                TestTable,
                                TestTable[A],
                                TestTable[B]
                                )
 
//Grouping the previous table by Column A only and taking count
VAR countColA =
GROUPBY(
            groupTable,
            [A],
            "CountA",            
            COUNTX(CURRENTGROUP(),[A])
)
 
//filtering table to return only values that have count of 1
VAR filterTable = FILTER(
                            countColA,
                            [CountA] = 1
)
 
//returning total count, which is 2 in sample data shared by you.
return COUNTX(filterTable, [A])

Thanks @talespin , it gives single occurrences of unique combinations of A and B.

v-yaningy-msft
Community Support
Community Support

Hi, @Tamillllll 

 

May I ask if this is the expected output you are looking for? Based on your description, I have created a new Table to achieve the effect you are looking for. Following picture shows the effect of the display.

vyaningymsft_0-1705976507539.png

vyaningymsft_1-1705976507541.png

vyaningymsft_3-1705976639782.png

New Table:

 

 

UniqueA =

SUMMARIZE (

    FILTER (

        ADDCOLUMNS (

            SUMMARIZE (

                'Table',

                'Table'[A],

                "UniqueValuesInB", DISTINCTCOUNT ( 'Table'[B] )

            ),

            "IsSingleValueInB", [UniqueValuesInB] = 1

        ),

        [IsSingleValueInB]

    ),

    [A]

)

 

 

 

If this does not work, could you please share some sample data without sensitive information and expected output.
How to provide sample data in the Power BI Forum - Microsoft Fabric Community

Best Regards,
Yang
Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

Hi yang,

 

Thank you sp much for the solution, I have explained in the below screenshot and also provided data below. 

 

Tamillllll_1-1705982503896.png

Data: 

 

JourneyIDStop SequenceVehicle IdRoute
189349413740000990  661
189349423740000990  661
189349433740000990  661
189349443740000990  661
189349453740000990  661
189349463740000990  661
18934947-1  661
190259013740000990  661
190259023740000990  661
190259033740000990  661
190259043740000990 661
19052771-1  661
19052772-1  661
19052773-1  661
19052774-1  661
19052775-1  661
19052776-1  661
19111511-1  661
19111512-1  661
19111513-1  661
19111514-1  661
19111515-1  661
19111516-1  661
lbendlin
Super User
Super User

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMrSwNDaxNFLSUTI2NzEwNDCwtDRQitWhkYSuIUTA0sDI1NIYWSXlEgizTY3MLWkuYGhoaGpEV4FYAA==", 
                            BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [A = _t, B = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"A", Int64.Type}, {"B", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"A"}, {{"CountD", each Table.RowCount(Table.Distinct(_)), Int64.Type}}),
    #"Filtered Rows" = Table.SelectRows(#"Grouped Rows", each ([CountD] = 1))
in
    #"Filtered Rows"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

Thank you so much for the solution, however its giving count of distinct values in B. I want to distinctcount column A when all its values is -1. As provided above in dax.

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.

Top Solution Authors