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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
test2sa
Helper I
Helper I

loop through table to replace certain values? for each loop? not sure how to approach issue

Not sure how to approach this in PowerBI. If this were a normal imperative programming language, I would just write a for-each loop.

 

I have an excel sheet of data in a Power BI report.

Quote Number   Winner      Shop         External Shop

145                      Y               ABC           

145                      N              TES

145                      N              CAR

647                      N              CAR

647                      Y               TES

647                      N              ABC

785                      Y               ABC

546                      N              TES            FEF

 

There's a bunch of quote offers from different shops and sometimes even an external shop. Only one shop can win each quote. 

 

I was thinking that I could create a new column in PowerBI datasource called "Winner Name".

Then what I would like to do is loop thru each quote to assign the winner instead of just displaying Y/N.

 

For each QuoteNumber,

If Winner = "Y", then for that QuoteNumber, grab Shop "ABC" and set WinnerName = ABC

and

If ExternalShop != blank, then WinnerName = FEF

 

The end results I want is:

 

Quote Number   Winner      Shop         External Shop     Winner Name

145                      Y               ABC                                       ABC

145                      N              TES                                        ABC

145                      N              CAR                                       ABC

647                      N              CAR                                       TES

647                      Y               TES                                        TES

647                      N              ABC                                       TES

785                      Y               ABC                                       ABC

546                      N              TES            FEF                       FEF

 

I'm trying to make it so if a shop filters for their Shop name, the table on my report displays who they lost to on all of their quotes.

Winner Name would immediately show them if they won or who they lost to.

 

How do I do something like this in PowerBI?

 

1 ACCEPTED SOLUTION
AntrikshSharma
Super User
Super User

@test2sa I assume you are trying to add a calculated column to the data model? In that case you can use this:

 

WinnerName = 
VAR Winner = 
    CALCULATE ( 
        SELECTEDVALUE ( 'Table'[Shop] ),
        'Table'[Winner] = "Y",
        ALLEXCEPT ( 'Table', 'Table'[Quote Number] )
    )
RETURN 
    IF ( 
        'Table'[External Shop] = "FEF",
        "FEF",
        Winner
    )

 

 

AntrikshSharma_0-1718903604288.png

 

View solution in original post

4 REPLIES 4
AntrikshSharma
Super User
Super User

@test2sa I assume you are trying to add a calculated column to the data model? In that case you can use this:

 

WinnerName = 
VAR Winner = 
    CALCULATE ( 
        SELECTEDVALUE ( 'Table'[Shop] ),
        'Table'[Winner] = "Y",
        ALLEXCEPT ( 'Table', 'Table'[Quote Number] )
    )
RETURN 
    IF ( 
        'Table'[External Shop] = "FEF",
        "FEF",
        Winner
    )

 

 

AntrikshSharma_0-1718903604288.png

 

Thank you!! I've almost got it working. I'm having trouble with that IF statement at the very end.

 

IF (
   NOT(ISBLANK('Table'[External Shop])),
      'Table'[External Shop],
     Winner
)

I've tried writing it a bunch of different ways and it wants to return only Winner or only External Shop. It won't return the correct one for each quote. 

@test2sa  you can do IF ( 'Table'[External Shop] <> "",  'Table'[External Shop] , Winner )

Thank you!! That worked perfectly!

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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