<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Search a String for 4 different variables then do a calculation based upon results in DAX Commands and Tips</title>
    <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Search-a-String-for-4-different-variables-then-do-a-calculation/m-p/774409#M3845</link>
    <description>&lt;P&gt;I am trying to search a column of product with multiple values for the first two letters of that column then do a calculation:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Product Code Column has values:&lt;/P&gt;&lt;P&gt;AR BLK&lt;/P&gt;&lt;P&gt;AR BLKMB&lt;/P&gt;&lt;P&gt;AR UHP&lt;/P&gt;&lt;P&gt;OX UHP&lt;/P&gt;&lt;P&gt;OX USP&lt;/P&gt;&lt;P&gt;NI NLK&lt;/P&gt;&lt;P&gt;NI FGBLK&lt;/P&gt;&lt;P&gt;etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then if the Product Code = "AR" multiply that by a value&lt;/P&gt;&lt;P&gt;If the Product Code = "NI"&amp;nbsp;multiply that by a different value&lt;/P&gt;&lt;P&gt;If the Product Code = OX"&amp;nbsp;multiply that by a different value (Quantity / # Trips)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's where I'm failing, I believe I need to use the LEFT function; but it failed as well:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;TPT = if(HASONEVALUE('DeliveryPoint'[ProductCode]), SWITCH(VALUES('DeliveryPoint'[ProductCode]),"NI", DIVIDE(SUM('Shifts and Events'[DeliveredQuantity(DU)])*0.0000362318840580, [Trips(#)]), "AR", DIVIDE(SUM('Shifts and Events'[DeliveredQuantity(DU)])*0.0000517063081696, [Trips(#)]), "OX", DIVIDE(SUM('Shifts and Events'[DeliveredQuantity(DU)])*0.0000413907284768, [Trips(#)]), "CO", DIVIDE(SUM('Shifts and Events'[DeliveredQuantity(DU)])/2000, [Trips(#)])))&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 23 Aug 2019 14:18:33 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2019-08-23T14:18:33Z</dc:date>
    <item>
      <title>Search a String for 4 different variables then do a calculation based upon results</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Search-a-String-for-4-different-variables-then-do-a-calculation/m-p/774409#M3845</link>
      <description>&lt;P&gt;I am trying to search a column of product with multiple values for the first two letters of that column then do a calculation:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Product Code Column has values:&lt;/P&gt;&lt;P&gt;AR BLK&lt;/P&gt;&lt;P&gt;AR BLKMB&lt;/P&gt;&lt;P&gt;AR UHP&lt;/P&gt;&lt;P&gt;OX UHP&lt;/P&gt;&lt;P&gt;OX USP&lt;/P&gt;&lt;P&gt;NI NLK&lt;/P&gt;&lt;P&gt;NI FGBLK&lt;/P&gt;&lt;P&gt;etc.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Then if the Product Code = "AR" multiply that by a value&lt;/P&gt;&lt;P&gt;If the Product Code = "NI"&amp;nbsp;multiply that by a different value&lt;/P&gt;&lt;P&gt;If the Product Code = OX"&amp;nbsp;multiply that by a different value (Quantity / # Trips)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here's where I'm failing, I believe I need to use the LEFT function; but it failed as well:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;TPT = if(HASONEVALUE('DeliveryPoint'[ProductCode]), SWITCH(VALUES('DeliveryPoint'[ProductCode]),"NI", DIVIDE(SUM('Shifts and Events'[DeliveredQuantity(DU)])*0.0000362318840580, [Trips(#)]), "AR", DIVIDE(SUM('Shifts and Events'[DeliveredQuantity(DU)])*0.0000517063081696, [Trips(#)]), "OX", DIVIDE(SUM('Shifts and Events'[DeliveredQuantity(DU)])*0.0000413907284768, [Trips(#)]), "CO", DIVIDE(SUM('Shifts and Events'[DeliveredQuantity(DU)])/2000, [Trips(#)])))&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 14:18:33 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Search-a-String-for-4-different-variables-then-do-a-calculation/m-p/774409#M3845</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2019-08-23T14:18:33Z</dc:date>
    </item>
    <item>
      <title>Re: Search a String for 4 different variables then do a calculation based upon results</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Search-a-String-for-4-different-variables-then-do-a-calculation/m-p/774429#M3848</link>
      <description>&lt;P&gt;you want to create a measure, correct?&lt;/P&gt;&lt;PRE&gt;TPT =
IF (
    HASONEVALUE ( 'DeliveryPoint'[ProductCode] ),
    SWITCH (
        LEFT ( FIRSTNONBLANK ( 'DeliveryPoint'[ProductCode], TRUE () ), 2 ),
        "NI", DIVIDE (
            SUM ( 'Shifts and Events'[DeliveredQuantity(DU)] ) * 0.0000362318840580, [Trips(#)] ),
        "AR", DIVIDE (
            SUM ( 'Shifts and Events'[DeliveredQuantity(DU)] ) * 0.0000517063081696, [Trips(#)] ),
        "OX", DIVIDE (
            SUM ( 'Shifts and Events'[DeliveredQuantity(DU)] ) * 0.0000413907284768, [Trips(#)] ),
        "CO", DIVIDE (&lt;BR /&gt;            SUM ( 'Shifts and Events'[DeliveredQuantity(DU)] ) / 2000, [Trips(#)] )
    )
)&lt;/PRE&gt;&lt;P&gt;For a calculated column this code could be simplified&lt;/P&gt;</description>
      <pubDate>Fri, 23 Aug 2019 14:32:36 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Search-a-String-for-4-different-variables-then-do-a-calculation/m-p/774429#M3848</guid>
      <dc:creator>Stachu</dc:creator>
      <dc:date>2019-08-23T14:32:36Z</dc:date>
    </item>
    <item>
      <title>Re: Search a String for 4 different variables then do a calculation based upon results</title>
      <link>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Search-a-String-for-4-different-variables-then-do-a-calculation/m-p/774877#M3886</link>
      <description>&lt;PRE&gt;// Please do yourself a favour and
// first create a measure (can be hidden):

[DU Total Code-Agnostic] = SUM ( 'Shifts and Events'[DeliveredQuantity(DU)] )

// Then create a table that will map
// the 2-letter codes into factors
// (the numbers 0.0000362318840580...).
// This could be best done in Power Query.
// You can call the table CodeToFactorMapping.

// Then create measures (as many of them&lt;BR /&gt;// as the number of 2-letter codes, where&lt;BR /&gt;// xx stands for such a code, NI, OX...):

[DU Total xx] =
var __factor = &amp;lt;get the factor from the mapping table for code xx using lookupvalue&amp;gt;
return
	[DU Total Code-Agnostic] * __factor

// Your final measure would be something
// like this:

TPT =
var __productCode = SELECTEDVALUE ( 'DeliveryPoint'[ProductCode] )
var __2letterCode = LEFT ( __productCode, 2 )
var __total = 
	SWITCH ( __2letterCode,
	
        "NI", [DU Total NI],
        "AR", [DU Total AR],
        "OX", [DU Total OX],
        "CO", [DU Total CO]
    )
var __trips = [Trips(#)]
var __tpt =
	divide ( __total, __trips )
return
	__tpt&lt;/PRE&gt;
&lt;P&gt;Please always try to keep your code clean. If there's something that repeats itself, make it into a unit on its own. You'll save yourself time and frustration and when a time comes to change, you'll need to make the change in one place only.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best&lt;/P&gt;
&lt;P&gt;Darek&lt;/P&gt;</description>
      <pubDate>Sat, 24 Aug 2019 12:57:54 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/DAX-Commands-and-Tips/Search-a-String-for-4-different-variables-then-do-a-calculation/m-p/774877#M3886</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2019-08-24T12:57:54Z</dc:date>
    </item>
  </channel>
</rss>

