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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
Anonymous
Not applicable

Single-cell stock allocations

I am looking for a solution preferably in DAX, or alternatively in M/Power Query.

 

My dataset consists of a number of investors' stock portfolios. The number of stocks in each portfolio varies across investors (e.g. one may invest in 2 stocks, another in 10), and so do their allocations towards each.

 

Each portfolio is stated in a single row in a single column (B), in the following format: Stock 1/5%:Stock 2/10%:Stock 3/85%. Of course the stocks have actual names, this is simply an example.

 

While the number of stocks in a portfolio is variable, the range of stocks investors can choose is fixed and can be categorized as either Europe, U.S., or Asia stocks.

 

Ultimately, what I want to calculate is the allocation towards each of these 3 regions (e.g. may Investor X have 10% in European stocks, 85% in American, and 5% in Asian). My current solution in Excel is to extract the stock names, lookup their region in a lookup table, and then sum the amounts in each.

 

However, my formula is not the most straight-forward, and I cannot seem to convert it to DAX, as I am now moving my calculations into PowerPivot. Here is the formula: 

=SUMPRODUCT(TEXT(TRIM(MID(SUBSTITUTE($B4&$G$4:$G$15;"%";REPT(" ";100));FIND($G$4:$G$15;SUBSTITUTE($B4&$G$4:$G$15;"%";REPT(" ";100)))+LEN($G$4:$G$15&"/");100));"0,000;0;0;\0")*($H$4:$H$15="U.S."))/100

 

Unfortunately I have not been able to attach the sample workbook, but instead I have enclosed an image of the sheet below. Thanks a lot in advance, let me know if you need further.

 

Sample dataset.png

1 ACCEPTED SOLUTION

@Anonymous ,

 

I've updated the file:  Download PBIX 

 

Capture.PNG

 

Ricardo



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



View solution in original post

15 REPLIES 15
camargos88
Community Champion
Community Champion

Hi @Anonymous ,

 

Check this file: Download PBIX 

 

Now just input the other tables and relate it.

 

If you consider it as a solution, please mark as a solution and kudos.

Ricardo



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



Anonymous
Not applicable

Great, definitely works better.

 

One issue, however. As mentioned, I know the range of stocks available, however, they won't be called "stock 1", "stock 2" etc., but instead "Amazon", "Facebook", etc.

 

How I look at it, this step is going to be an issue. Any suggestions around this? I have a list of all the stocks available (similar to the lookup table in the sample) classified by region.

 

PowerBI.png

 

@Anonymous ,

 

Now you can import the table with the stock name and relate them.

Also if you want, import the stock name and merge it with the initial table. If you need help with that, just paste the table name here.

 

Ricardo



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



Anonymous
Not applicable

I am not sure I understand.

 

Since the "Added column" step relies on finding the name "Stock", won't my query break as no stocks are actually called "stock", but rather "Amazon", etc.?

 

Best,

Phill

 

 

@Anonymous ,

 

Based on your initial post, how do you know the name of stock 1 ?

I just see table with region that we can use to map it.

 

Ricardo



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



Anonymous
Not applicable

I have a list of e.g. 100 stocks that investors can choose from.

 

However, which ones the investors actually choose (and how many) I won't know in advance. All I know is that the stock name will appear in the formatting mentioned.

 

So, a stock in the sample looking something like:

  • Stock 1/5%/5000:Stock 2/10%/10000:Stock 3/85%/85000

 

Would be something like this in the real dataset:

  • Amazon/5%/30000:Facebook/10%/10000:Apple/85%/80000

 

The key here is the format: Stock / allocation in % / allocation i USD. Does it make sense?

 

Here is an adjusted sample:

 

Table 1: Portfolios

CPRPortfolio
Investor 1Amazon/5%:Facebook/10,315%:Apple/85%
Investor 2Amazon/30%/30000:Uber/20%/20000/Johnson & Johnson/50%/50000
Investor 3VISA/40%/4000:Microsoft/20%/2000/Amazon/30%/3000:Facebook/10%/1000
Investor 4Amazon/5%/500,25:Facebook/10%/1000,50:Apple/85%/8004
Investor 5Amazon/30%/30000:Uber/20%/20000/Johnson & Johnson/50%/50000
Investor 6VISA/40%/4000:Microsoft/20%/2000/Amazon/30%/3000:Facebook/10%/1000
Investor 7Amazon/5%/500,25:Facebook/10%/1000,50:Apple/85%/8004
Investor 8Amazon/10%/1000:Facebook/60%/6000:Uber/20%/2000:JPMorgan/10%/1000
Investor 9VISA/40%/4000:Microsoft/20%/2000/Amazon/30%/3000:Facebook/10%/1000
Investor 10Amazon/5%/500,25:Facebook/10%/1000,50:Apple/85%/8004
Investor 11Amazon/30%/30000:Uber/20%/20000/Johnson & Johnson/50%/50000
Investor 12VISA/40%/4000:Microsoft/20%/2000/Amazon/30%/3000:Facebook/10%/1000

 

Table 2: Lookup table (ignore the regions)

NameRegion
AmazonAsia
FacebookU.S.
AppleEurope
MicrosoftAsia
UberU.S.
JPMorganU.S.
VISAEurope
Johnson & JohnsonAsia
AlphabetU.S.
IBMU.S.
TeslaU.S.
TwitterEurope

@Anonymous ,

 

I've updated the file:  Download PBIX 

 

Capture.PNG

 

Ricardo



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



Anonymous
Not applicable

Excellent, worked a charm.

 

One last enquiry. Is there anyway to reach the end result directly in PowerQuery.

 

So, instead of:

Current result.png

 

I'd prefer: 

Sample result.png

 

The reason being that I have additional data where keeping the row context is ideal.

Hi @Anonymous ,

 

It's possible, however you can do it easily using matrix. Also this format you are gonna have more workaround for calculations.

 

Ricardo



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



@Anonymous ,

 

Yes, once you have the real data, it will split by your delimiters ( / and : )

 

Ricardo



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



Anonymous
Not applicable

Would you care to update the file based on the tables I just added?

 

I still cannot seem to make it work for some reason haha.

 

Sorry for the inconvenience!

Anonymous
Not applicable

Hi again,

 

Works well in PowerBi, thanks.

 

However, I am actually working in Excel (2013) with PowerQuery/PowerPivot add-ins (sorry if I was unclear about this), and for some reason, I do not have the "split into rows" option.PowerQuery extract.png

 

What to do in this case?

 

Best,

Phil

Hi @Anonymous ,

 

I've updated the file with another table. This new code has another approach to handle it.

 

I hope it works.

 

Ricardo



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



camargos88
Community Champion
Community Champion

Hi @Anonymous ,

 

I believe that using power query you can split each stock in rows.

So you can relate the tables.

 

Can you paste the values here ?

 

Ricardo



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



Anonymous
Not applicable

I agree, but I am not sure whether it will work, as I do not know the number of stocks in each portfolio beforehand and hence neither whether to split across 5, 10, 20 columns etc.

 

Anyways, if you have a flexible solution, I'd really appreciate your input. Here is the sample data.

 

Investor 1Stock 1/5%:Stock 2/10%:Stock 3/85%
Investor 2Stock 1/30%/30000:Stock 5/20%/20000/Stock 8/50%/50000
Investor 3Stock 7/40%/4000:Stock 4/20%/2000/Stock 1/30%/3000:Stock 2/10%/1000
Investor 4Stock 1/5%/500,25:Stock 2/10%/1000,50:Stock 3/85%/8004
Investor 5Stock 1/30%/30000:Stock 5/20%/20000/Stock 8/50%/50000
Investor 6Stock 7/40%/4000:Stock 4/20%/2000/Stock 1/30%/3000:Stock 2/10%/1000
Investor 7Stock 1/5%/500,25:Stock 2/10%/1000,50:Stock 3/85%/8004
Investor 8Stock 1/10%/1000:Stock 2/60%/6000:Stock 5/20%/2000:Stock 6/10%/1000
Investor 9Stock 7/40%/4000:Stock 4/20%/2000/Stock 1/30%/3000:Stock 2/10%/1000
Investor 10Stock 1/5%/500,25:Stock 2/10%/1000,50:Stock 3/85%/8004
Investor 11Stock 1/30%/30000:Stock 5/20%/20000/Stock 8/50%/50000
Investor 12Stock 7/40%/4000:Stock 4/20%/2000/Stock 1/30%/3000:Stock 2/10%/1000

 

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.