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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
thmonte
Helper IV
Helper IV

Creating a value column for each time an item appears within a single date

I have a table that has the first 4 columns here.  I want to try and create a new column that breaks the "items' down to everytime they appear within a date range regardless of "Store".

 

so COUNT how many times a distinct item appears in "Item 1" OR Item 2" within a single day and divide it by 1 to give it a value.  Then once each distinct item has a value add the two for that specific row to come up with the "Total" column.

 

 

DateStoreItem 1Item 2ValueTotal
11/12/2017Store AItem AItem B.25 + .330.58
11/12/2017Store AItem AItem B.25 + .330.58
11/12/2017Store AItem AItem C.25 + .500.75
11/12/2017Store BItem Anull.250.25
11/12/2017Store CItem BItem C.33 + .500.83
11/12/2017Store CItem Dnull11
     3.99

 

I wasn't really sure what to research on this as I feel its a pretty unique case.  Any help would be appreciated.

1 ACCEPTED SOLUTION

Hi @thmonte,

 

You could create a measure similar to:

Column3 =
VAR myCount1 =
    IF (
        Table3[Region] IN { "UK", "US" },
        0,
        (
            CALCULATE (
                COUNTROWS ( 'Table3' ),
                FILTER (
                    ALLEXCEPT ( 'Table3', Table3[Date] ),
                    Table3[Item 1] = EARLIER ( 'Table3'[Item 1] )
                        && Table3[Region] <> "US"
                        && Table3[Region] <> "UK"
                )
            )
                + CALCULATE (
                    COUNTROWS ( 'Table3' ),
                    FILTER (
                        ALLEXCEPT ( 'Table3', Table3[Date] ),
                        Table3[Item 2] = EARLIER ( 'Table3'[Item 1] )
                            && Table3[Region] <> "US"
                            && Table3[Region] <> "UK"
                    )
                )
        )
    )
VAR myCount1percent =
    IF ( 'Table3'[Item 1] <> "null", DIVIDE ( 1 / myCount1, 1 ), 0 )
VAR myCount2 =
    IF (
        Table3[Region] IN { "UK", "US" },
        0,
        (
            CALCULATE (
                COUNTROWS ( 'Table3' ),
                FILTER (
                    ALLEXCEPT ( 'Table3', Table3[Date] ),
                    Table3[Item 1] = EARLIER ( 'Table3'[Item 2] )
                        && Table3[Region] <> "US"
                        && Table3[Region] <> "UK"
                )
            )
                + CALCULATE (
                    COUNTROWS ( 'Table3' ),
                    FILTER (
                        ALLEXCEPT ( 'Table3', Table3[Date] ),
                        Table3[Item 2] = EARLIER ( 'Table3'[Item 2] )
                            && Table3[Region] <> "US"
                            && Table3[Region] <> "UK"
                    )
                )
        )
    )
VAR myCount2percent =
    IF ( Table3[Item 2] <> "null", DIVIDE ( 1 / myCount2, 1 ), 0 )
RETURN
    myCount2percent + myCount1percent

1.PNG

 

Best regards,

Yuliana Gu

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

View solution in original post

5 REPLIES 5
v-yulgu-msft
Microsoft Employee
Microsoft Employee

Hi @thmonte,

 

I made a little modification to Phil_Seamark's solution to take the date into account. Please try:

Column = 

VAR myCount1 = CALCULATE(COUNTROWS('Table3'),FILTER(ALLEXCEPT('Table3',Table3[Date]),Table3[Item 1] = EARLIER('Table3'[Item 1])))
             + CALCULATE(COUNTROWS('Table3'),FILTER(ALLEXCEPT('Table3',Table3[Date]),Table3[Item 2] = EARLIER('Table3'[Item 1])))
             
VAR myCount1percent = IF ('Table3'[Item 1] <> blank(), DIVIDE( 1 /   myCount1,1 ),0)

VAR myCount2 = CALCULATE(COUNTROWS('Table3'),FILTER(ALLEXCEPT('Table3',Table3[Date]),Table3[Item 1] = EARLIER('Table3'[Item 2])))
             + CALCULATE(COUNTROWS('Table3'),FILTER(ALLEXCEPT('Table3',Table3[Date]),Table3[Item 2] = EARLIER('Table3'[Item 2]))) 

VAR myCount2percent = IF(Table3[Item 2] <> BLANK(), DIVIDE( 1 / myCount2 , 1),0)

RETURN myCount2percent + myCount1percent

Replace ALL with ALLEXCEPT when calculating count values for each item.

 

Regards,
Yuliana Gu

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

This worked great!  Thank you for the response.  If I wanted to take this one step further and filter out regions that = US and UK and not have them get counted, would that be possible?

 

 

 

DateRegionStoreItem 1Item 2ValueTotal
11/12/2017USStore AItem AItem B 0.58
11/12/2017USStore AItem AItem B 0.58
11/12/2017UKStore AItem AItem C 0.75
11/12/2017CAStore BItem Anull11
11/12/2017CAStore CItem BItem C1 + 12
11/12/2017CAStore CItem Dnull11
       
    Final Count 4

 

 

Hi @thmonte,

 

You could create a measure similar to:

Column3 =
VAR myCount1 =
    IF (
        Table3[Region] IN { "UK", "US" },
        0,
        (
            CALCULATE (
                COUNTROWS ( 'Table3' ),
                FILTER (
                    ALLEXCEPT ( 'Table3', Table3[Date] ),
                    Table3[Item 1] = EARLIER ( 'Table3'[Item 1] )
                        && Table3[Region] <> "US"
                        && Table3[Region] <> "UK"
                )
            )
                + CALCULATE (
                    COUNTROWS ( 'Table3' ),
                    FILTER (
                        ALLEXCEPT ( 'Table3', Table3[Date] ),
                        Table3[Item 2] = EARLIER ( 'Table3'[Item 1] )
                            && Table3[Region] <> "US"
                            && Table3[Region] <> "UK"
                    )
                )
        )
    )
VAR myCount1percent =
    IF ( 'Table3'[Item 1] <> "null", DIVIDE ( 1 / myCount1, 1 ), 0 )
VAR myCount2 =
    IF (
        Table3[Region] IN { "UK", "US" },
        0,
        (
            CALCULATE (
                COUNTROWS ( 'Table3' ),
                FILTER (
                    ALLEXCEPT ( 'Table3', Table3[Date] ),
                    Table3[Item 1] = EARLIER ( 'Table3'[Item 2] )
                        && Table3[Region] <> "US"
                        && Table3[Region] <> "UK"
                )
            )
                + CALCULATE (
                    COUNTROWS ( 'Table3' ),
                    FILTER (
                        ALLEXCEPT ( 'Table3', Table3[Date] ),
                        Table3[Item 2] = EARLIER ( 'Table3'[Item 2] )
                            && Table3[Region] <> "US"
                            && Table3[Region] <> "UK"
                    )
                )
        )
    )
VAR myCount2percent =
    IF ( Table3[Item 2] <> "null", DIVIDE ( 1 / myCount2, 1 ), 0 )
RETURN
    myCount2percent + myCount1percent

1.PNG

 

Best regards,

Yuliana Gu

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

This looks really close but almost all my numbers are off by .5.

 

In the simplest scenario item 1 is unique once and item 2 is unique once the expression should return 2.0.  In your example it is returning 1.5

 

Edit: disregard, i believe there was just a syntax error

 

 

Phil_Seamark
Microsoft Employee
Microsoft Employee

HI @thmonte

 

This is close and works on your sample data.  It probably needs some tweaking around the use of the ALL in the FITLER functions but that should be easy enough.

 

Column = 

VAR myCount1 = CALCULATE(COUNTROWS('Table3'),FILTER(ALL('Table3'),Table3[Item 1] = EARLIER('Table3'[Item 1])))
             + CALCULATE(COUNTROWS('Table3'),FILTER(ALL('Table3'),Table3[Item 2] = EARLIER('Table3'[Item 1])))
             
VAR myCount1percent = IF ('Table3'[Item 1] <> blank(), DIVIDE( 1 /   myCount1,1 ),0)

VAR myCount2 = CALCULATE(COUNTROWS('Table3'),FILTER(ALL('Table3'),Table3[Item 1] = EARLIER('Table3'[Item 2])))
             + CALCULATE(COUNTROWS('Table3'),FILTER(ALL('Table3'),Table3[Item 2] = EARLIER('Table3'[Item 2]))) 

VAR myCount2percent = IF(Table3[Item 2] <> BLANK(), DIVIDE( 1 / myCount2 , 1),0)

RETURN myCount2percent + myCount1percent

To learn more about DAX visit : aka.ms/practicalDAX

Proud to be a Datanaut!

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.