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

Count the common Item Codes which exists in the previous year row, in the same data table?

Hi, I am trying to count the items which exists in the current year, and also existed in the previous year. The sample data is given below. As you can see from the data, I would like to find out say in 2019, we had a total item count of 6, and those matching with 2018's item codes are 3. 

 

Sample Data
Item CodeDateSale Price
131/01/2017$3
630/04/2017$25
330/05/2017$31
530/06/2017$12
231/07/2017$10
431/10/2017$34
1031/01/2018$45
130/09/2018$21
1128/02/2018$24
928/02/2018$51
431/03/2018$46
230/06/2018$121
331/07/2018$106
131/07/2019$12
231/01/2019$34
330/06/2019$45
1530/08/2019$42
1630/06/2019$24
1730/09/2019$100
131/12/2020$32
1531/01/2020$54
1629/02/2020$45
2029/02/2020$67
2131/03/2020$32
2230/06/2020$11

 

The final result, I would like to see is like this:

YearCurrent CountMatched with Next 12 Months
201764
201872
201963
20206-

 

I'm able to do this in excel with VLOOKUP function, but not sure how to do it in Power BI.

201720182018 Item codes matching with 201720192019 Item codes matching with 201820202020 Item codes matching with 2019
1331#N/A11
222221515
344331616
49#N/A15#N/A20#N/A
510#N/A16#N/A21#N/A
611#N/A17#N/A22#N/A
 11    

 

I have used the calculated column earlier, but it is not giving me the right results for each year going in the past Years.

 

 

Existing Item from Previous Year = 
var _count = 
COUNTROWS(
    FILTER(
        ALL('Sample Data'),
        [Item Code] = EARLIER('Sample Data'[Item Code]) &&
        'Sample Data'[Date] = YEAR(TODAY())-1 &&
    )
)
return
IF(
    ISBLANK(_count),
    0,
    1
)

 

 

 

Any help would be much appreciated!

Thanks

1 ACCEPTED SOLUTION

Hi @sushstorm ,

 

Maybe you can try this code,this has the view of months by drill down:

Matched with Next 12 Months or next month =
VAR _cur_m_t = // current month
    GROUPBY(
        FILTER(
            ALL( 'Table' ),
            DATE( YEAR( [Date] ), MONTH( [Date] ), 1 )
                = DATE( SELECTEDVALUE( 'Table'[Date].[Year] ), SELECTEDVALUE( 'Table'[Date].[MonthNo] ), 1 )
        ),
        'Table'[Item Code]
    )
VAR _nex_m_t = // next month
    GROUPBY(
        FILTER(
            ALL( 'Table' ),
            DATE( YEAR( [Date] ), MONTH( [Date] ), 1 )
                = DATE( SELECTEDVALUE( 'Table'[Date].[Year] ), SELECTEDVALUE( 'Table'[Date].[MonthNo] ) + 1, 1 )
        ),
        'Table'[Item Code]
    )
VAR _cur_y_t = // current year
    GROUPBY(
        FILTER(
            ALL( 'Table' ),
            [Date].[Year] = SELECTEDVALUE( 'Table'[Date].[Year] )
        ),
        'Table'[Item Code]
    )
VAR _nex_y_t = // next year
    GROUPBY(
        FILTER(
            ALL( 'Table' ),
            [Date].[Year]
                = SELECTEDVALUE( 'Table'[Date].[Year] ) + 1
        ),
        'Table'[Item Code]
    )
VAR _match_m =
    COUNTROWS( _cur_m_t ) - COUNTROWS( EXCEPT( _cur_m_t, _nex_m_t ) )
VAR _match_y =
    COUNTROWS( _cur_y_t ) - COUNTROWS( EXCEPT( _cur_y_t, _nex_y_t ) )
RETURN
    IF( SELECTEDVALUE( 'Table'[Date].[Month] ) = BLANK(), _match_y, _match_m )

Result:

vchenwuzmsft_0-1642747036286.png

(2018 and 2019, they all have item code 1,but why you expect result is 2)

Pbix in the end you can refer.

Best Regards

Community Support Team _ chenwu zhu

 

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

4 REPLIES 4
sushstorm
Frequent Visitor

Hi @amitchandak

Thanks for your reply, but in this case, I'm getting the following message:

Last year Sales: FUNCTION 'DATEADD' expects a contiguous selection when the date column is not unique, has gaps or it contains time portion.

 

Also, is there any chance I can get the view with Months and Years, instead of just years in the final results?

Thanks

Hi @sushstorm ,

 

Maybe you can try this code,this has the view of months by drill down:

Matched with Next 12 Months or next month =
VAR _cur_m_t = // current month
    GROUPBY(
        FILTER(
            ALL( 'Table' ),
            DATE( YEAR( [Date] ), MONTH( [Date] ), 1 )
                = DATE( SELECTEDVALUE( 'Table'[Date].[Year] ), SELECTEDVALUE( 'Table'[Date].[MonthNo] ), 1 )
        ),
        'Table'[Item Code]
    )
VAR _nex_m_t = // next month
    GROUPBY(
        FILTER(
            ALL( 'Table' ),
            DATE( YEAR( [Date] ), MONTH( [Date] ), 1 )
                = DATE( SELECTEDVALUE( 'Table'[Date].[Year] ), SELECTEDVALUE( 'Table'[Date].[MonthNo] ) + 1, 1 )
        ),
        'Table'[Item Code]
    )
VAR _cur_y_t = // current year
    GROUPBY(
        FILTER(
            ALL( 'Table' ),
            [Date].[Year] = SELECTEDVALUE( 'Table'[Date].[Year] )
        ),
        'Table'[Item Code]
    )
VAR _nex_y_t = // next year
    GROUPBY(
        FILTER(
            ALL( 'Table' ),
            [Date].[Year]
                = SELECTEDVALUE( 'Table'[Date].[Year] ) + 1
        ),
        'Table'[Item Code]
    )
VAR _match_m =
    COUNTROWS( _cur_m_t ) - COUNTROWS( EXCEPT( _cur_m_t, _nex_m_t ) )
VAR _match_y =
    COUNTROWS( _cur_y_t ) - COUNTROWS( EXCEPT( _cur_y_t, _nex_y_t ) )
RETURN
    IF( SELECTEDVALUE( 'Table'[Date].[Month] ) = BLANK(), _match_y, _match_m )

Result:

vchenwuzmsft_0-1642747036286.png

(2018 and 2019, they all have item code 1,but why you expect result is 2)

Pbix in the end you can refer.

Best Regards

Community Support Team _ chenwu zhu

 

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

Hello, 

Thanks for your solution. I had to tweek it a little bit to get to my solution, but it eventually worked! 

I changed the following section of your code:

VAR _nex_m_t = // next month GROUPBY( FILTER( ALL( 'Table' ), DATE( YEAR( [Date] ), MONTH( [Date] ), 1 ) = DATE( SELECTEDVALUE( 'Table'[Date].[Year] ), SELECTEDVALUE( 'Table'[Date].[MonthNo] ) + 12, 1 ) ), 'Table'[Item Code]
amitchandak
Super User
Super User

@sushstorm , With help from date table create meausre like first 2 or Next two


YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD('Date'[Date],"12/31"))
Last YTD Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(dateadd('Date'[Date],-1,Year),"12/31"))

or


This year Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(ENDOFYEAR('Date'[Date]),"12/31"))
Last year Sales = CALCULATE(SUM(Sales[Sales Amount]),DATESYTD(ENDOFYEAR(dateadd('Date'[Date],-1,Year)),"12/31"))


Then check for


countx(values(Table[Item Code]), if(not(isblank([This year Sales])) && not(isblank([Last year Sales])) , [Item Code], blank()))

 

 

Very similar logic

https://community.powerbi.com/t5/Community-Blog/Customer-Retention-Part-1-Month-on-Month-Retention/b...

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.