<?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: Apply expectations conditionally in SLDP in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/apply-expectations-conditionally-in-sldp/m-p/150810#M53523</link>
    <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/213812"&gt;@mauriciofh&lt;/a&gt;&amp;nbsp;. You are right. I should add/remove the validation rules from the rule dictionary.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 13 Mar 2026 13:25:40 GMT</pubDate>
    <dc:creator>hdu</dc:creator>
    <dc:date>2026-03-13T13:25:40Z</dc:date>
    <item>
      <title>Apply expectations conditionally in SLDP</title>
      <link>https://community.databricks.com/t5/data-engineering/apply-expectations-conditionally-in-sldp/m-p/150629#M53485</link>
      <description>&lt;P&gt;Hi folks,&amp;nbsp;&lt;/P&gt;&lt;P&gt;The following code runs as expected, and all three rules are validated.&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; @&lt;/SPAN&gt;&lt;SPAN&gt;dp&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;view&lt;/SPAN&gt;&lt;SPAN&gt;(&lt;/SPAN&gt;&lt;SPAN&gt;name&lt;/SPAN&gt;&lt;SPAN&gt;=&lt;/SPAN&gt;&lt;SPAN&gt;f&lt;/SPAN&gt;&lt;SPAN&gt;"v_validate_source_&lt;/SPAN&gt;&lt;SPAN&gt;{&lt;/SPAN&gt;&lt;SPAN&gt;table&lt;/SPAN&gt;&lt;SPAN&gt;}&lt;/SPAN&gt;&lt;SPAN&gt;"&lt;/SPAN&gt;&lt;SPAN&gt;)&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; @&lt;/SPAN&gt;&lt;SPAN&gt;dp&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;expect_all_or_drop&lt;/SPAN&gt;&lt;SPAN&gt;({&lt;/SPAN&gt;&lt;SPAN&gt;"201-Data row"&lt;/SPAN&gt;&lt;SPAN&gt;:&lt;/SPAN&gt;&lt;SPAN&gt;"row_cnt &amp;gt; 0"&lt;/SPAN&gt;&lt;SPAN&gt;})&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; @&lt;/SPAN&gt;&lt;SPAN&gt;dp&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;expect_all_or_drop&lt;/SPAN&gt;&lt;SPAN&gt;({&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;"101-One footer row"&lt;/SPAN&gt;&lt;SPAN&gt; : &lt;/SPAN&gt;&lt;SPAN&gt;"footer_cnt = 1"&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;"102-Row count mismatch"&lt;/SPAN&gt;&lt;SPAN&gt; : &lt;/SPAN&gt;&lt;SPAN&gt;"footer_row_cnt = row_cnt"&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; })&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;def&lt;/SPAN&gt; &lt;SPAN&gt;validateSourceFileView&lt;/SPAN&gt;&lt;SPAN&gt;():&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; return df&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;My question is how can I apply rule 201 check conditionally? Something like:&lt;/P&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; if condition:&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;@&lt;/SPAN&gt;&lt;SPAN&gt;dp&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;expect_all_or_drop&lt;/SPAN&gt;&lt;SPAN&gt;({&lt;/SPAN&gt;&lt;SPAN&gt;"201-Data row"&lt;/SPAN&gt;&lt;SPAN&gt;:&lt;/SPAN&gt;&lt;SPAN&gt;"row_cnt &amp;gt; 0"&lt;/SPAN&gt;&lt;SPAN&gt;})&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; @&lt;/SPAN&gt;&lt;SPAN&gt;dp&lt;/SPAN&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;SPAN&gt;expect_all_or_drop&lt;/SPAN&gt;&lt;SPAN&gt;({&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;"101-One footer row"&lt;/SPAN&gt;&lt;SPAN&gt; : &lt;/SPAN&gt;&lt;SPAN&gt;"footer_cnt = 1"&lt;/SPAN&gt;&lt;SPAN&gt;,&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;&lt;SPAN&gt;"102-Row count mismatch"&lt;/SPAN&gt;&lt;SPAN&gt; : &lt;/SPAN&gt;&lt;SPAN&gt;"footer_row_cnt = row_cnt"&lt;/SPAN&gt;&lt;/DIV&gt;&lt;DIV&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; })&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;Thank you for your help in advance.&lt;/P&gt;</description>
      <pubDate>Wed, 11 Mar 2026 19:41:09 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/apply-expectations-conditionally-in-sldp/m-p/150629#M53485</guid>
      <dc:creator>hdu</dc:creator>
      <dc:date>2026-03-11T19:41:09Z</dc:date>
    </item>
    <item>
      <title>Re: Apply expectations conditionally in SLDP</title>
      <link>https://community.databricks.com/t5/data-engineering/apply-expectations-conditionally-in-sldp/m-p/150630#M53486</link>
      <description>&lt;P&gt;Great question. With decorators, you cannot place them inside an if block the way you wrote. Decorators are applied when the function is defined.&lt;/P&gt;&lt;P&gt;The clean way is to build the expectations dictionary first, then apply one decorator:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;rules = {
    "101-One footer row": "footer_cnt = 1",
    "102-Row count mismatch": "footer_row_cnt = row_cnt",
}

if condition:
    rules["201-Data row"] = "row_cnt &amp;gt; 0"

@dp.view(name=f"v_validate_source_{table}")
@dp.expect_all_or_drop(rules)
def validateSourceFileView():
    return df&lt;/LI-CODE&gt;&lt;P&gt;Important detail: condition must be known at pipeline-definition time (for example, table name, config flag, pipeline parameter), not per-row runtime logic.&lt;/P&gt;&lt;P&gt;If you need row-level conditional behavior, encode it in the rule expression itself, for example:&lt;/P&gt;&lt;LI-CODE lang="python"&gt;"201-Data row": "NOT apply_201 OR row_cnt &amp;gt; 0"&lt;/LI-CODE&gt;&lt;P&gt;&lt;SPAN&gt;where apply_201 is a boolean column/flag in the dataframe. This keeps one rule but makes it conditional per record&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 11 Mar 2026 20:36:49 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/apply-expectations-conditionally-in-sldp/m-p/150630#M53486</guid>
      <dc:creator>mauriciofh</dc:creator>
      <dc:date>2026-03-11T20:36:49Z</dc:date>
    </item>
    <item>
      <title>Re: Apply expectations conditionally in SLDP</title>
      <link>https://community.databricks.com/t5/data-engineering/apply-expectations-conditionally-in-sldp/m-p/150810#M53523</link>
      <description>&lt;P&gt;Thank you&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/213812"&gt;@mauriciofh&lt;/a&gt;&amp;nbsp;. You are right. I should add/remove the validation rules from the rule dictionary.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 13 Mar 2026 13:25:40 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/apply-expectations-conditionally-in-sldp/m-p/150810#M53523</guid>
      <dc:creator>hdu</dc:creator>
      <dc:date>2026-03-13T13:25:40Z</dc:date>
    </item>
  </channel>
</rss>

