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
EdList
Helper I
Helper I

Lookup with 1toN connection

Hi,

 

I am searching since a week but cannot find a working solution. Even if it seems a simple problem.

 

I have two tables which have a 1 to n relationship:

The first has data about Location. Each location has a ID.

The other table contains reports of visits from those locations with date and customer information. Thus a location can be visited more often.

 

I wanted to visualize "visited locations" VS "not visited locations" in a bar chart. I thought it would be simple but it did not work.

So I used a lookup-function to check if the location has been visited:

 

Visited = IF(LOOKUPVALUE(ID_Besuchsbericht_Kunde[PoiID_Split.2],ID_Besuchsbericht_Kunde[PoiID_Split.2],poi[poiId])<>0,"yes","no"
 
This works fine with the new column.
But now I want to filter in my chart by month and see if a location has been visited f.e. in January.
This is where I am stuck. I thought I can just use a simple relativ date slicer to get this working. However it did not work. Probobly because of the lookup function...
 
Thus I thought ok then just apply a "AND" statement and make 12 new columns to check if visited "January", "February", ...
This looks like that:
 
February visited = IF(AND(LOOKUPVALUE(ID_Besuchsbericht_Kunde[PoiID_Split.2],ID_Besuchsbericht_Kunde[PoiID_Split.2],poi[poiId])<>0, LOOKUPVALUE(ID_Besuchsbericht_Kunde[month], ID_Besuchsbericht_Kunde[PoiID_Split.2],poi[poiId])>1),"yes","no")
 
This does not work because the second lookupvalue return more than one value.
 
Somehow I am stuck. Can somebody help?
I hope I explained it properly.
 
Thanks.
 
Jakob
1 ACCEPTED SOLUTION
Icey
Community Support
Community Support

Hi @EdList ,

 

You want to show counts in bar chart, right?

If so, you need to create another table with "Yes" and "No".

Then, create the count measure like so:

 

Measure 2 =
VAR YN =
    SELECTEDVALUE ( 'Table'[Column1] )
RETURN
    CALCULATE ( COUNT ( Shops[ID] ), FILTER ( Shops, [Visited] = YN ) )

 

coun.PNG

 

 

Best Regards,

Icey

 

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

13 REPLIES 13
Greg_Deckler
Super User
Super User

@EdList - It's difficult to understand what is going on from your description. Please first check if your issue is a common issue listed here: https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

Also, please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490

The most important parts are:
1. Sample data as text, use the table tool in the editing bar
2. Expected output from sample data
3. Explanation in words of how to get from 1. to 2.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_DecklerSorry but my post yesterday has been deleted somehow....

 

I try to explain in more detail and easier:

 

I have two tables:

 

Table 1 - Shops

 

IDShop nameShop LocationVisited with vlookup
1234BradstoreViennayes
4332Ninastore

Berlin

yes

3212Samstore

Spain

no

 

Table 2 - Visits

 

SalespersonVisit-IDDateID Store visited
Tom322313-01-2020

1234

Jack321314-02-20201234
Sam321201-04-20204332
Eva212301-05-20201234

 

So the tables have a 1 to n connection.

 

I wanted to show in a chart how many stores are visited and how many are not. As a location can be visited more often a normal vlookup would fail. So i just make a new column to check if the store has been visited.

 

Lookup:

visited = IF(LOOKUPVALUE(table1[id],table2[visit-id],table1[id])=0,"no","yes")

This works fine so far.

 

My problem is that i want to make a date-slicer to check if a certain location has been visited in a certain month.

Any clue how to get this to work??

 

Thanks.

 

Jakob

 

 

You are better off getting your result through a measure over a calculated column.  Here are the steps:

 

1. Make a relationship between your Shops and Visits tables on the StoreID column (1:many from Shops to Visits)

2. Add a table visual with Store ID, Shop Name, and Shop Location and the measure in #3.  Make sure you use the columns from the Shops table.

3. Make a measure like this and add it to the table visual

Visited = IF(ISBLANK(Countrows(Visits), "No", "Yes")

4. Add a Date table to your model, so you can slice by Date info.  Make a 1:Many relationship from it to the Date column of your Visits table. See this article for more details - https://www.sqlbi.com/articles/creating-a-simple-date-table-in-dax/

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Hi @mahoneypat 

 

thank you very much for your help.

Your solution is exactely what I had already. However I am happy that you got my problem right 🙂

I check if a store is visited by the this calculated column:

visited = if(ISBLANK(COUNTROWS(RELATEDTABLE(visits))), "no", "yes")

Except for the date table. I added it but the calculated visits visuals still does not change when i filter with the slicer.

 

 

 

 

 

asdf.PNG

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Thanks

 

Jakob

 

Your last response states that you put your expression in a calculated column.  My response provided a measure expression.  Measures respond to slicer selection, while calculated columns do not.  Please try using the measure expression I provided, along with the other suggestions for tables and relationships.


Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


@mahoneypatThanks. I use a measure now and when using table or a matrix visual it works perfectly fine also when slicing by date!!

I use Shop-ID as value and the measure is used in the quickinfo.

 

However it fails using a chart visual because it does not show any result. Any idea for this? Than everything would be solved for me 🙂

Unbenannt.PNG


Thank you in advance.

 

Jakob

Icey
Community Support
Community Support

Hi @EdList ,

 

You want to show counts in bar chart, right?

If so, you need to create another table with "Yes" and "No".

Then, create the count measure like so:

 

Measure 2 =
VAR YN =
    SELECTEDVALUE ( 'Table'[Column1] )
RETURN
    CALCULATE ( COUNT ( Shops[ID] ), FILTER ( Shops, [Visited] = YN ) )

 

coun.PNG

 

 

Best Regards,

Icey

 

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

Hi @Icey 

 

thanks again for your soluation. Works fine.

However I am struggeling now showing the visits in a bar chart where the visits are splitted by group. My data looks like this now:

 

meassure22bygroup.PNG

 

I have two tables "Visits" and "Shoplist" as shown on the left side. I applyied your solution and got the bottom right chart which is correct. I also can filter by date which works perfectly fine as well.

 

What I want now is to visualize the visits splitted by the Shopgroup (chart in the upper right corner).

 

I tryied several other measures but none work.

 

Any suggestions?

 

Thanks.

 

Jakob

Icey
Community Support
Community Support

Hi @EdList ,

 

Please check if this is what you want:

 

Put "Shopgroup" column into "Axis" field.

group.gif

 

BTW, .pbix file attached.

 

 

Best Regards,

Icey

 

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

Hi @Icey ,

 

thanks again. Yes this is what I want. However this brings up another problem: I cannot sort by number of shops (number of ID's).

You got a solution for that?

 

My pbix is available here: https://filebin.net/qd8v5yp216r9cy2u

.

 

As you can see on the picture the expected result should be EAS>MES>SEP>IAS because I sort by the quickinfo 'number of ID (shops)'. I guess it does not work due to the measure?

 

sort.PNG

 

Thanks again.

 

BG Jakob

Icey
Community Support
Community Support

Hi @EdList ,

 

Create another measure like so:

Measure = CALCULATE ( COUNT ( Shoplist[ID] ), ALL ( 'YN-Table'[YN] ) )

 

And then use this measure to sort.

sort.jpg

 

 

Best Regards,

Icey

 

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

 

Great thanks!!

@Greg_Deckler  Thank you for your reply. Yes I checked the links. I think I am just bad in explaining.

Maybe I just tell you what I want to visualize:

 

As i mentioned I have two tables:

table 1

IDWhere were you?CustomerInfoDate
22342ViennaFranksold him something2020-01-23
23421BerlinTomjust drank coffee2020-02-01
22342ViennaFranksold him something2020-03-01
22342ViennaFranksold him something 

table 2

IDTownvisited*
22342Viennayes
23421

Berlin

yes

23431

Spain

no

 

This is an 1 to n connection.

 

What I want is to show in a chart which IDs from table 2 have been visited. As I had no clue how to visualize that I just added a lookup-function in table 2:

visited = IF(LOOKUPVALUE(table1[ID],table1[ID],table2[ID])=0,"no","yes")
 
This worked.
 
However I thought I could do it without a lookup function but did not know how.
Now I want to have a date slicer. This is what causes me the problem. I did find a way to work around the problem but it is so inefficient that I cannot use it.
 
There has to be a way to do this easier?
 
Hope I explained better now.

Thanks.
 
Jakob

 

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.