<?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 Re: Help Needed with Formula Firewall Issue and Running Python Script in Power BI in Developer</title>
    <link>https://community.fabric.microsoft.com/t5/Developer/Help-Needed-with-Formula-Firewall-Issue-and-Running-Python/m-p/4033710#M54080</link>
    <description>&lt;P&gt;Hi&amp;nbsp; &lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/587365"&gt;@COS019970&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This error occurs when the formula firewall is not functioning properly due to the structure of the query, you can check the related contentlink for the solution:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Add references to the advanced editor to the query&lt;/LI&gt;
&lt;LI&gt;Ignore privacy level&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Refer to:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://community.fabric.microsoft.com/t5/Power-Query/Query-references-other-queries-or-steps-so-it-may-not-directly/td-p/2836287" target="_blank"&gt;Solved: Query references other queries or steps, so it may... - Microsoft Fabric Community&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the related document, you can view this content:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://community.fabric.microsoft.com/t5/Desktop/Formula-Firewall-Query-references-other-queries-so-it-may-not/td-p/18619/page/2" target="_blank"&gt;Solved: Formula.Firewall: Query references other queries, ... - Page 2 - Microsoft Fabric Community&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://stackoverflow.com/questions/72725587/powerbi-formula-firewall-error-on-loading-python-script" target="_blank"&gt;powerquery - Powerbi Formula.Firewall Error on loading python script - Stack Overflow&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://stackoverflow.com/questions/42227329/power-query-please-rebuild-this-data-combination" target="_blank"&gt;powerquery - Power Query - Please rebuild this data combination - Stack Overflow&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;/P&gt;
&lt;P&gt;Liu Yang&lt;/P&gt;
&lt;P&gt;If this post &lt;STRONG&gt;helps&lt;/STRONG&gt;, then please consider &lt;EM&gt;Accept it as the solution&lt;/EM&gt; to help the other members find it more quickly.&lt;/P&gt;</description>
    <pubDate>Wed, 10 Jul 2024 05:21:17 GMT</pubDate>
    <dc:creator>Anonymous</dc:creator>
    <dc:date>2024-07-10T05:21:17Z</dc:date>
    <item>
      <title>Help Needed with Formula Firewall Issue and Running Python Script in Power BI</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Help-Needed-with-Formula-Firewall-Issue-and-Running-Python/m-p/4032064#M54054</link>
      <description>&lt;P&gt;I'm encountering an issue with the Formula Firewall in Power BI when trying to combine data from multiple sources and run a Python script. I have two separate data sources that I want to combine and process using Python. Here are the details:&lt;/P&gt;&lt;H3&gt;Data Sources:&lt;/H3&gt;&lt;OL&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;Contract Mating:&lt;/STRONG&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;This data source contains details about contract mating.&lt;/LI&gt;&lt;LI&gt;Sample fields: Dam, HERD_Dam, Returned, Year, Sire, AI_herd_Check, DIY_Herd_Check, Source.&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;LI&gt;&lt;P&gt;&lt;STRONG&gt;Contract Mating Invoiced Sales:&lt;/STRONG&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;This data source contains sales information for contract mating.&lt;/LI&gt;&lt;LI&gt;Sample fields: BTE Number, Item, Total DIY Quantity purchased, Average cost per straw Amount, Source.&lt;/LI&gt;&lt;/UL&gt;&lt;/LI&gt;&lt;/OL&gt;&lt;P&gt;This is the error message I am getting: Formula.Firewall: Query 'CombinedData' (step 'Run Python script') references other queries or steps, so it may not directly access a data source. Please rebuild this data combination.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# 'dataset' holds the input data for this script
import pandas as pd
import numpy as np

# The dataset provided by Power BI should be directly accessible as a DataFrame named 'dataset'
data = dataset

# Verify the structure of the dataset
print(data.head())

# Split the combined dataset into 'Credit' and 'Contract' DataFrames based on the 'Source' column
Credit = data[data['Source'] == 'Contract Mating Invoiced Sales'].copy()
Contract = data[data['Source'] == 'Contract Mating'].copy()

# Bring back all the herds in the contract file
Contracts = Contract.reset_index(drop=True)

# Calculate the number of dams that received each sire recommendation per herd
Contract_dams_Herd_sire = Contracts.groupby(['HERD_Dam', 'Sire']).agg({'Dam': ['count']}).reset_index()
Contract_dams_Herd_sire.columns = ['HERD_Dam', 'Sire', 'No_of_Dams_Recommended_to_sire']
Contract_dams_Herd_sire['Max_number_of_straws_to_credit_per_sire'] = Contract_dams_Herd_sire['No_of_Dams_Recommended_to_sire'] * 2

# Calculate the number of dams recommended for matings in each herd
Contract_dams_herd = Contracts.groupby('HERD_Dam')['Dam'].nunique().reset_index()
Contract_dams_herd.columns = ['HERD_Dam', 'No_of_Dams_Contracted_to_herd']
Contract_dams_herd['Max_number_of_straws_to_credit_per_herd'] = Contract_dams_herd['No_of_Dams_Contracted_to_herd'] * 2

# Merge summary herd stats
Contract_summary = pd.merge(left=Contract_dams_Herd_sire, right=Contract_dams_herd, how='outer', left_on='HERD_Dam', right_on='HERD_Dam')

# Merge in herd stats with the sales figures from IT
Contract_Credit = pd.merge(left=Credit, right=Contract_summary, how='outer', left_on=['HERD_Dam', 'Sire'], right_on=['HERD_Dam', 'Sire'])

# Replace any sires who were contracted but have not been purchased to 0
Contract_Credit['Total DIY Quantity purchased'] = Contract_Credit['Total DIY Quantity purchased'].fillna(0)
# Replace any sires who were contracted but have no value to 0
Contract_Credit['Average cost per straw Amount'] = Contract_Credit['Average cost per straw Amount'].fillna(0)

# Calculate the maximum number of straws to credit based on purchases
def potential_straw_credit(x):
    if x['Max_number_of_straws_to_credit_per_sire'] &amp;gt;= x['Total DIY Quantity purchased']:
        return x['Total DIY Quantity purchased']
    else:
        return x['Max_number_of_straws_to_credit_per_sire']

Contract_Credit['Potential_Straws_to_credit'] = Contract_Credit.apply(potential_straw_credit, axis=1)

# Sort by the average cost of the straw per herd so that you end up crediting for the more expensive ones first
Contract_Credit = Contract_Credit.sort_values(by=['HERD_Dam', 'Average cost per straw Amount'], ascending=False).reset_index(drop=True)

# Cumulative sum the straws that were purchased off what needs to be credited
Contract_Credit['cumsum_herd'] = Contract_Credit.groupby(['HERD_Dam'])['Potential_Straws_to_credit'].cumsum()
Contract_Credit['previous'] = Contract_Credit.groupby(['HERD_Dam'])['cumsum_herd'].shift(1).fillna(0)

# Function to work out should the purchase be credited - any minus will be blanked as enough has been credited
def Cumsum(x):
    if x['cumsum_herd'] &amp;lt;= x['Max_number_of_straws_to_credit_per_herd']:
        return x['Potential_Straws_to_credit']
    elif x['cumsum_herd'] &amp;gt; x['Potential_Straws_to_credit']:
        return x['Potential_Straws_to_credit'] - x['previous']
    else:
        return 0

Contract_Credit['Straws_to_credit'] = Contract_Credit.apply(Cumsum, axis=1)
Contract_Credit['Straws_to_credit'] = np.where(Contract_Credit['Straws_to_credit'] &amp;lt; 0, 0, Contract_Credit['Straws_to_credit'])

# Calculate the amount to credit
Contract_Credit['Amount_to_credit'] = Contract_Credit['Straws_to_credit'] * Contract_Credit['Average cost per straw Amount']

# Summarize the total credit amount per herd
Contract_Credit_Per_Herd = Contract_Credit.groupby(['HERD_Dam']).agg({'Amount_to_credit': ['sum']}).reset_index()
Contract_Credit_Per_Herd.columns = ['HERD_Dam', 'Total_Credit_Amount (€)']

# Output final DataFrame to Power BI
result = Contract_Credit_Per_Herd&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="CombinedData Table" style="width: 999px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/1128954i21E2859C9F256AD5/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="CombinedData Table" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;CombinedData Table&lt;/span&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="CombinedData Table" style="width: 758px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/1128955i07CE9AB9A136D56E/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="CombinedData Table" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;CombinedData Table&lt;/span&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Contract Mating Invoiced Sales Table" style="width: 999px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/1128958i8519E8BB84409D66/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="Contract Mating Invoiced Sales Table" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;Contract Mating Invoiced Sales Table&lt;/span&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="CombinedData Table" style="width: 999px;"&gt;&lt;img src="https://community.fabric.microsoft.com/t5/image/serverpage/image-id/1128968i4B98A70A3AA0E467/image-size/large?v=v2&amp;amp;px=999" role="button" title="CombinedData Table.png" alt="CombinedData Table" /&gt;&lt;span class="lia-inline-image-caption" onclick="event.preventDefault();"&gt;CombinedData Table&lt;/span&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 09 Jul 2024 08:21:53 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Help-Needed-with-Formula-Firewall-Issue-and-Running-Python/m-p/4032064#M54054</guid>
      <dc:creator>COS019970</dc:creator>
      <dc:date>2024-07-09T08:21:53Z</dc:date>
    </item>
    <item>
      <title>Re: Help Needed with Formula Firewall Issue and Running Python Script in Power BI</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Help-Needed-with-Formula-Firewall-Issue-and-Running-Python/m-p/4033710#M54080</link>
      <description>&lt;P&gt;Hi&amp;nbsp; &lt;a href="https://community.fabric.microsoft.com/t5/user/viewprofilepage/user-id/587365"&gt;@COS019970&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This error occurs when the formula firewall is not functioning properly due to the structure of the query, you can check the related contentlink for the solution:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Add references to the advanced editor to the query&lt;/LI&gt;
&lt;LI&gt;Ignore privacy level&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Refer to:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://community.fabric.microsoft.com/t5/Power-Query/Query-references-other-queries-or-steps-so-it-may-not-directly/td-p/2836287" target="_blank"&gt;Solved: Query references other queries or steps, so it may... - Microsoft Fabric Community&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is the related document, you can view this content:&lt;/P&gt;
&lt;P&gt;&lt;A href="https://community.fabric.microsoft.com/t5/Desktop/Formula-Firewall-Query-references-other-queries-so-it-may-not/td-p/18619/page/2" target="_blank"&gt;Solved: Formula.Firewall: Query references other queries, ... - Page 2 - Microsoft Fabric Community&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://stackoverflow.com/questions/72725587/powerbi-formula-firewall-error-on-loading-python-script" target="_blank"&gt;powerquery - Powerbi Formula.Firewall Error on loading python script - Stack Overflow&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="https://stackoverflow.com/questions/42227329/power-query-please-rebuild-this-data-combination" target="_blank"&gt;powerquery - Power Query - Please rebuild this data combination - Stack Overflow&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best Regards,&lt;/P&gt;
&lt;P&gt;Liu Yang&lt;/P&gt;
&lt;P&gt;If this post &lt;STRONG&gt;helps&lt;/STRONG&gt;, then please consider &lt;EM&gt;Accept it as the solution&lt;/EM&gt; to help the other members find it more quickly.&lt;/P&gt;</description>
      <pubDate>Wed, 10 Jul 2024 05:21:17 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Help-Needed-with-Formula-Firewall-Issue-and-Running-Python/m-p/4033710#M54080</guid>
      <dc:creator>Anonymous</dc:creator>
      <dc:date>2024-07-10T05:21:17Z</dc:date>
    </item>
    <item>
      <title>Re: Help Needed with Formula Firewall Issue and Running Python Script in Power BI</title>
      <link>https://community.fabric.microsoft.com/t5/Developer/Help-Needed-with-Formula-Firewall-Issue-and-Running-Python/m-p/4042139#M54181</link>
      <description>&lt;P&gt;Thank you so much!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jul 2024 09:20:59 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Developer/Help-Needed-with-Formula-Firewall-Issue-and-Running-Python/m-p/4042139#M54181</guid>
      <dc:creator>COS019970</dc:creator>
      <dc:date>2024-07-15T09:20:59Z</dc:date>
    </item>
  </channel>
</rss>

