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

SQL to DAX

Hi - I have 2 tables that I imported into my Power BI.  One is Households and one is named Contacts.  Each contact is linked to a Household.  I want to add 2 columns to the Household Table that are called Head_1_Name and Head_2_Name.  

 

The SQL to do this in our Database is:

Households.Household_Name ,(SELECT Top 1 First_Name FROM Contacts C WHERE C.Household_ID = Households.Household_ID and Household_Position_ID = 1 ORDER BY Gender_ID) AS Head_1_Name ,(SELECT Top 1 First_Name FROM Contacts C WHERE C.Household_ID = Households.Household_ID and Household_Position_ID = 1 AND EXISTS (SELECT 1 FROM Contacts C2 WHERE C2.Household_Position_ID = 1 AND C2.Household_ID = C.Household_ID GROUP BY Household_ID HAVING Count(*) > 1) GROUP BY Contact_ID, First_Name, Gender_ID ORDER BY Gender_ID Desc) AS Head_2_Name
 
Is there a way to do this in Power BI, and where would I include this SQL?  Thanks!
3 REPLIES 3
v-stephen-msft
Community Support
Community Support

Hi @mdamico ,

 

Could you tell me if your problem has been solved?

If it is, kindly Accept it as the solution. More people will benefit from it.

In fact, after you add a new column in SQL Server, you click the refresh button in Power Query, and the data will be refreshed.

23.png

 

 

Best Regards,

Stephen Tao

 

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

CNENFRNL
Community Champion
Community Champion

Value.NativeQuery() comes to your rescue; you may pass the whole intact sql to it. Another wonderful blog in this regard for your reference.


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Greg_Deckler
Super User
Super User

@mdamico Sample data would be infinitely more helpful than SQL code. You didn't read this:
https://community.powerbi.com/t5/Community-Blog/Before-You-Post-Read-This/ba-p/1116882

In any case, probably the best thing to do would be to do this in Power Query via a Merge query.

 

But, you could something like DAX as well. One way would be to create an intermediate column like:

Column = 
  VAR __ID = [Household_ID]
RETURN
  CONCATENATEX(FILTER(Contacts,Contacts[Houshold_ID] = __ID),[Contact_ID] & [First_Name] & [Gender_ID],",")

You could then split this column out via SEARCH, LEFT, MID, etc. functions based on the comma. Again, not entirely sure what you are going for as I hate reading other people's SQL code.


@ 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...

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.

Top Solution Authors
Top Kudoed Authors