[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M699GCWWBZM2XFNW5PTGRFJP":3,"skill-same:01M699GCWWBZM2XFNW5PTGRFJP":63},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":16,"categories":17,"keywords":28,"publishedAt":35,"linkOnly":36,"markdownContent":37,"markdownBody":38,"frontMatter":39,"refName":40,"commitSha":41,"skillDirectory":42,"forksCount":43,"repositoryHtmlUrl":44,"repositoryHomepageUrl":6,"ownerHtmlUrl":45,"ownerBlogUrl":46,"ownerDescription":47,"updatedAt":48,"install":49,"resources":62},"01M699GCWWBZM2XFNW5PTGRFJP","sql_to_dax",null,"Guide for translating SQL aggregation expressions into DAX measures.","microsoft\u002Fsemantic-link-labs","microsoft","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F6154722?v=4","01M698VYDXZ6JW5WH3CRV650R8","Microsoft","01M699FKMT63C17BFNH8WHZN2K",600,"MIT","https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fsemantic-link-labs\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Fsql_to_dax\u002FSKILL.md",[18,23],{"categoryNo":19,"categoryName":20,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000010","后端与接口","SCENARIO","使用场景",{"categoryNo":24,"categoryName":25,"taxonomyType":26,"taxonomyTypeDesc":27},"01JBSEEDCAT000000000000033","数据分析师","PROFESSION","适用职业",[29,30,31,32,33,34],"dax","sql to dax","sql","aggregation","guide","for","2026-11-10 10:12:30",false,"---\nname: sql_to_dax\ndescription: Guide for translating SQL aggregation expressions into DAX measures.\n---\n\n# SKILL.md — SQL to DAX Metric Translation\n\n## Purpose\n\nTranslate analytical SQL aggregation expressions into equivalent DAX measures.\n\nThe goal is semantic equivalence, not syntactic similarity.\n\nThe generated DAX should:\n\n- Follow Power BI \u002F Tabular best practices\n- Prefer iterator functions when row context is required\n- Use DIVIDE instead of `\u002F NULLIF(...,0)`\n- Fully qualify columns using `'table'[column]`\n- Use CALCULATE where filter context translation is required\n- Preserve aggregation semantics exactly\n- Avoid SQL constructs unsupported in DAX by rewriting logically\n\n---\n\n# SQL Identifier Parsing Rules\n\nThe SQL source may reference columns using any of the following formats:\n\n```sql\ncolumn_name\n```\n\n```sql\ntable.column_name\n```\n\n```sql\ntable.`column name`\n```\n\nThe translator must normalize all forms into valid DAX column references.\n\n---\n\n## Identifier Normalization Rules\n\n| SQL Format | DAX Format |\n|---|---|\n| column_name | 'table'[column_name] |\n| table.column_name | 'table'[column_name] |\n| table.`column name` | 'table'[column name] |\n\n---\n\n## Backtick Handling\n\nSQL backticks must be removed during translation.\n\n### SQL\n```sql\ndim_product.`standard cost`\n```\n\n### DAX\n```dax\n'dim_product'[standard cost]\n```\n\n---\n\n## Unqualified Column Resolution\n\nIf a column is referenced without a table qualifier:\n\n```sql\nSUM(SALES_AMOUNT)\n```\n\nThe translator should:\n\n1. Infer the table from model metadata if available\n2. Prefer the primary fact table in the expression\n3. Fully qualify the final DAX output\n\n### DAX\n```dax\nSUM('fact_sales'[SALES_AMOUNT])\n```\n\n> **Important — bare identifiers inside scalar aggregates must NOT become iterators.**\n>\n> When a SQL aggregate wraps a single bare identifier (e.g. `SUM(ORIGINAL_SALES_AMOUNT)`,\n> `SUM(COST_OF_GOODS_SOLD)`) and that identifier is not declared as a\n> column in the model metadata, the translator must still emit the\n> scalar aggregation form against the default\u002Fowning table:\n>\n> ```dax\n> SUM('fact_returns'[ORIGINAL_SALES_AMOUNT])\n> ```\n>\n> It must **not** fall through to the iterator form:\n>\n> ```dax\n> SUMX('fact_returns', ORIGINAL_SALES_AMOUNT)   -- INVALID\n> ```\n>\n> Iterators (`SUMX`, `AVERAGEX`, etc.) are reserved for cases where the\n> aggregate argument contains arithmetic or references multiple\n> columns. A single bare token is always a scalar aggregation.\n\n---\n\n## Mixed Identifier Formats\n\nExpressions may mix styles.\n\n### SQL\n```sql\nSUM(fact_sales.`sales amount` - DISCOUNT_AMOUNT)\n```\n\n### DAX\n```dax\nSUMX(\n    'fact_sales',\n    'fact_sales'[sales amount] -\n    'fact_sales'[DISCOUNT_AMOUNT]\n)\n```\n\n---\n\n# Core Translation Rules\n\n---\n\n## 1. Aggregate Functions\n\n### SQL\n```sql\nSUM(column)\n```\n\n### DAX\n```dax\nSUM('table'[column])\n```\n\n---\n\n### SQL\n```sql\nAVG(column)\n```\n\n### DAX\n```dax\nAVERAGE('table'[column])\n```\n\n---\n\n### SQL\n```sql\nCOUNT(DISTINCT column)\n```\n\n### DAX\n```dax\nDISTINCTCOUNT('table'[column])\n```\n\n---\n\n## 2. Arithmetic Inside Aggregations\n\nIf arithmetic occurs inside SUM\u002FAVG\u002Fetc., use iterator functions.\n\n### SQL\n```sql\nSUM(price * quantity)\n```\n\n### DAX\n```dax\nSUMX(\n    'table',\n    'table'[price] * 'table'[quantity]\n)\n```\n\n---\n\n### SQL\n```sql\nSUM(revenue - discount)\n```\n\n### DAX\n```dax\nSUMX(\n    'table',\n    'table'[revenue] - 'table'[discount]\n)\n```\n\n---\n\n### SQL\n```sql\nAVG(quantity * cost)\n```\n\n### DAX\n```dax\nAVERAGEX(\n    'table',\n    'table'[quantity] * 'table'[cost]\n)\n```\n\n---\n\n# Iterator Function Rules\n\nUse iterator functions when:\n\n- Multiple columns participate in row-level arithmetic\n- Expressions exist inside aggregate functions\n- Mixed table references occur inside aggregation\n\n| SQL Aggregate | DAX Iterator |\n|---|---|\n| SUM(expr) | SUMX(table, expr) |\n| AVG(expr) | AVERAGEX(table, expr) |\n| MIN(expr) | MINX(table, expr) |\n| MAX(expr) | MAXX(table, expr) |\n\n---\n\n# Safe Division\n\n---\n\n## SQL NULLIF Pattern\n\n### SQL\n```sql\nSUM(sales) \u002F NULLIF(SUM(cost), 0)\n```\n\n### DAX\n```dax\nDIVIDE(\n    SUM('table'[sales]),\n    SUM('table'[cost])\n)\n```\n\n---\n\n## Nested NULLIF\n\n### SQL\n```sql\n365 \u002F NULLIF(metric, 0)\n```\n\n### DAX\n```dax\nDIVIDE(\n    365,\n    [metric]\n)\n```\n\n---\n\n# Percentage Calculations\n\n---\n\n### SQL\n```sql\n(metric \u002F total) * 100\n```\n\n### DAX\n```dax\nDIVIDE(\n    [metric],\n    [total]\n) * 100\n```\n\n---\n\n# ROUND Translation\n\n---\n\n### SQL\n```sql\nROUND(expression, 2)\n```\n\n### DAX\n```dax\nROUND(expression, 2)\n```\n\n---\n\n# DIV0 Translation\n\nDIV0 means divide-by-zero-safe division.\n\n### SQL\n```sql\nDIV0(a, b)\n```\n\n### DAX\n```dax\nDIVIDE(a, b)\n```\n\n---\n\n# Window Function Translation\n\n---\n\n## Rolling Window SUM\n\nTranslate `\u003Cagg>(\u003Cinner>) OVER (ORDER BY \u003Ccol> ROWS BETWEEN N PRECEDING AND CURRENT ROW)`\ninto a `CALCULATE` wrapping the inner aggregation with a `DATESINPERIOD`\nfilter over the ORDER BY column.\n\nRules:\n\n- The window's ORDER BY column becomes the date column passed to\n  `DATESINPERIOD`. It is resolved through the column map and fully\n  qualified as `'table'[column]`.\n- `MAX(\u003Corder_col>)` is used as the anchor date.\n- `ROWS BETWEEN N PRECEDING AND CURRENT ROW` becomes `-N, DAY`. The skill\n  preserves the literal `N` from the SQL (e.g. `89 PRECEDING` → `-89, DAY`)\n  rather than rounding up to the inclusive day count.\n- When the inner expression is itself an aggregate (a non-standard but\n  common Snowflake\u002FBigQuery pattern such as `SUM(SUM(...)) OVER (...)`),\n  the redundant outer aggregate is stripped and only the inner\n  aggregation body is preserved.\n- The inner aggregation body is translated using the normal aggregation\n  rules (including the additive distribution rule that turns\n  `SUM(a - b)` into `(SUM(a) - SUM(b))`).\n\n### SQL\n```sql\nSUM(metric)\nOVER (\n    ORDER BY DATE_KEY\n    ROWS BETWEEN 89 PRECEDING AND CURRENT ROW\n)\n```\n\n### DAX\n```dax\nCALCULATE(\n    [metric],\n    DATESINPERIOD(\n        'dim_date'[date_key],\n        MAX('dim_date'[date_key]),\n        -89,\n        DAY\n    )\n)\n```\n\n---\n\n## Rolling Window over a Nested Aggregate\n\n### SQL\n```sql\nSUM(\n    SUM(EXTENDED_AMOUNT - DISCOUNT_AMOUNT)\n) OVER (\n    ORDER BY DATE_KEY\n    ROWS BETWEEN 89 PRECEDING AND CURRENT ROW\n)\n```\n\n### DAX\n```dax\nCALCULATE(\n    (\n        SUM('fact_sales'[sales_amount])\n        - SUM('fact_sales'[discount_amount])\n    ),\n    DATESINPERIOD(\n        'dim_date'[date_key],\n        MAX('dim_date'[date_key]),\n        -89,\n        DAY\n    )\n)\n```\n\nEquivalently (when distribution is not applied), the inner body may be\nemitted in iterator form:\n\n```dax\nCALCULATE(\n    SUMX(\n        'fact_sales',\n        'fact_sales'[sales_amount] - 'fact_sales'[discount_amount]\n    ),\n    DATESINPERIOD(\n        'dim_date'[date_key],\n        MAX('dim_date'[date_key]),\n        -89,\n        DAY\n    )\n)\n```\n\n---\n\n## Unbounded Window\n\nAn unbounded window (`OVER ()` with no PARTITION BY, ORDER BY, or frame)\nignores the current filter context entirely. Translate it to the\n**iterator form of the aggregate over `ALL(\u003Ctable>)`** — not to\n`CALCULATE(\u003Cagg>, ALL(\u003Ctable>))`.\n\n| SQL aggregate | DAX |\n|---|---|\n| `SUM(col) OVER ()` | `SUMX(ALL('table'), 'table'[col])` |\n| `AVG(col) OVER ()` | `AVERAGEX(ALL('table'), 'table'[col])` |\n| `MIN(col) OVER ()` | `MINX(ALL('table'), 'table'[col])` |\n| `MAX(col) OVER ()` | `MAXX(ALL('table'), 'table'[col])` |\n| `COUNT(col) OVER ()` | `COUNTX(ALL('table'), 'table'[col])` |\n| `COUNT(*) OVER ()` | `COUNTROWS(ALL('table'))` |\n| `COUNT(DISTINCT col) OVER ()` | `CALCULATE(DISTINCTCOUNT('table'[col]), ALL('table'))` |\n\n### SQL\n```sql\nMAX(cutoff) OVER ()\n```\n\n### DAX\n```dax\nMAXX(ALL('table'), 'table'[cutoff])\n```\n\n### SQL\n```sql\nSUM(amount) OVER ()\n```\n\n### DAX\n```dax\nSUMX(ALL('table'), 'table'[amount])\n```\n\n---\n\n# CASE WHEN Translation\n\n---\n\n## Conditional DISTINCTCOUNT\n\n### SQL\n```sql\nCOUNT(DISTINCT CASE\n    WHEN order_count > 1\n    THEN customer_key\nEND)\n```\n\n### DAX\n```dax\nCALCULATE(\n    DISTINCTCOUNT('table'[customer_key]),\n    'table'[order_count] > 1\n)\n```\n\n---\n\n# Multi-Table Arithmetic\n\nWhen expressions reference multiple tables:\n\n- Preserve table qualification\n- Use iterator functions\n- Choose the fact table as the iterator table when possible\n\n---\n\n## Iterator Table Selection from Relationships\n\nWhen an X-function (`SUMX`, `AVERAGEX`, `COUNTX`, `MINX`, `MAXX`) is needed and\nthe inner expression references two tables that participate in a relationship,\nchoose the iterator table as the **\"from\" side** of that relationship\n(typically the many \u002F fact side). Wrap any column reference to the\n**\"to\" side** (typically the one \u002F dimension side) in `RELATED(...)`.\n\nThis rule is independent of the table that the measure is *defined on* — what\nmatters is which table is on the many side of the relationship linking the\ntwo referenced tables.\n\n### Example\n\nGiven a relationship: `fact_sales (Many) → dim_product (One)`\n\n### SQL\n```sql\nSUM(dim_product.standard_cost * fact_sales.quantity_sold)\n```\n\n### DAX\n```dax\nSUMX(\n    'fact_sales',\n    RELATED('dim_product'[standard_cost]) *\n    'fact_sales'[quantity_sold]\n)\n```\n\nEven when this measure is authored on the `dim_product` table, the iterator\ntable is still `fact_sales` because `fact_sales` is the \"from\" side of the\nrelationship.\n\n---\n\n### SQL\n```sql\nSUM(dim_product.standard_cost * fact_sales.quantity)\n```\n\n### DAX\n```dax\nSUMX(\n    'fact_sales',\n    RELATED('dim_product'[standard_cost]) *\n    'fact_sales'[quantity]\n)\n```\n\n---\n\n### SQL\n```sql\nSUM(\n    (EXTENDED_AMOUNT - DISCOUNT_AMOUNT)\n    - (STANDARD_COST * QUANTITY)\n)\n```\n\n### DAX\n```dax\nSUMX(\n    'fact_sales',\n    ('fact_sales'[EXTENDED_AMOUNT] - 'fact_sales'[DISCOUNT_AMOUNT])\n    -\n    (\n        RELATED('dim_product'[STANDARD_COST]) *\n        'fact_sales'[QUANTITY]\n    )\n)\n```\n\n---\n\n# KPI Translation Patterns\n\n---\n\n## Profit\n\n### SQL\n```sql\nSUM(revenue - cost)\n```\n\n### DAX\n```dax\nSUMX(\n    'fact',\n    'fact'[revenue] - 'fact'[cost]\n)\n```\n\n---\n\n## Margin %\n\n### SQL\n```sql\nSUM(profit)\n\u002F\nNULLIF(SUM(revenue), 0)\n```\n\n### DAX\n```dax\nDIVIDE(\n    [Profit],\n    [Revenue]\n)\n```\n\n---\n\n## Return Rate %\n\n### SQL\n```sql\nSUM(return_amount)\n\u002F\nNULLIF(SUM(original_sales_amount), 0)\n```\n\n### DAX\n```dax\nDIVIDE(\n    SUM('fact_returns'[return_amount]),\n    SUM('fact_sales'[original_sales_amount])\n)\n```\n\n---\n\n# Table Qualification Rules\n\n---\n\n## Always Qualify Columns\n\nPreferred:\n```dax\n'fact_sales'[sales_amount]\n```\n\nAvoid:\n```dax\n[sales_amount]\n```\n\n---\n\n# Relationship Translation\n\n---\n\n## SQL Join Semantics\n\nWhen SQL implies dimension lookup:\n\n### SQL\n```sql\ndim_product.standard_cost\n```\n\ninside fact aggregation becomes:\n\n### DAX\n```dax\nRELATED('dim_product'[standard_cost])\n```\n\n---\n\n# Translation Heuristics\n\n---\n\n## Detect Iterator Requirement\n\nUse X-iterators when:\n\n- Expression contains operators inside aggregation\n- More than one column appears inside SUM\u002FAVG\u002Fetc.\n- Arithmetic mixes dimensions and facts\n\n---\n\n## Detect Measure References\n\nNested aggregates inside a window function are flattened — the redundant\nouter aggregate is dropped and only the inner aggregate body is wrapped\nin `CALCULATE`.\n\n### SQL\n```sql\nSUM(SUM(revenue)) OVER (\n    ORDER BY DATE_KEY\n    ROWS BETWEEN 89 PRECEDING AND CURRENT ROW\n)\n```\n\n### DAX\n```dax\nCALCULATE(\n    SUM('fact_sales'[revenue]),\n    DATESINPERIOD(\n        'dim_date'[date_key],\n        MAX('dim_date'[date_key]),\n        -89,\n        DAY\n    )\n)\n```\n\n---\n\n# Common SQL → DAX Mappings\n\n| SQL | DAX |\n|---|---|\n| SUM(col) | SUM(table[col]) |\n| AVG(col) | AVERAGE(table[col]) |\n| COUNT(DISTINCT col) | DISTINCTCOUNT(table[col]) |\n| NULLIF(x,0) | DIVIDE(... ) |\n| ROUND(x,n) | ROUND(x,n) |\n| CASE WHEN | CALCULATE\u002FFILTER |\n| OVER(...) | CALCULATE + time intelligence |\n| SUM(a*b) | SUMX(table,a*b) |\n\n---\n\n# Example Translations\n\n---\n\n## Example 1\n\n### SQL\n```sql\nSUM(store_sales.ss_sales_price * store_sales.ss_quantity)\n```\n\n### DAX\n```dax\nSUMX(\n    'store_sales',\n    'store_sales'[ss_sales_price] *\n    'store_sales'[ss_quantity]\n)\n```\n\n---\n\n## Example 2\n\n### SQL\n```sql\nSUM(ATTRIBUTED_REVENUE)\n\u002F\nNULLIF(SUM(SPEND_AMOUNT), 0)\n```\n\n### DAX\n```dax\nDIVIDE(\n    SUM('fact_marketing'[ATTRIBUTED_REVENUE]),\n    SUM('fact_marketing'[SPEND_AMOUNT])\n)\n```\n\n---\n\n## Example 3\n\n### SQL\n```sql\n(\n  COUNT(DISTINCT CASE\n    WHEN CUSTOMER_ORDER_COUNT > 1\n    THEN CUSTOMER_KEY\n  END)\n  \u002F\n  NULLIF(COUNT(DISTINCT CUSTOMER_KEY), 0)\n) * 100\n```\n\n### DAX\n```dax\nDIVIDE(\n    CALCULATE(\n        DISTINCTCOUNT('customer'[CUSTOMER_KEY]),\n        'customer'[CUSTOMER_ORDER_COUNT] > 1\n    ),\n    DISTINCTCOUNT('customer'[CUSTOMER_KEY])\n) * 100\n```\n\n---\n\n# Output Requirements\n\nGenerated DAX must:\n\n- Be valid Power BI DAX syntax\n- Use proper indentation\n- Use uppercase DAX functions\n- Prefer DIVIDE over `\u002F`\n- Prefer iterators over invalid scalar arithmetic\n- Preserve business semantics exactly\n- Avoid unnecessary CALCULATE wrappers\n- Use RELATED for dimension attribute access\n- Use measures when semantic reuse is implied\n\n---\n\n# Important Semantic Differences\n\nSQL is row-set based.\n\nDAX is filter-context based.\n\nCorrect translation often requires:\n\n- Iterator functions\n- Context transition\n- Relationship navigation\n- Measure decomposition\n\nDo not attempt direct token replacement.\n\n","# SKILL.md — SQL to DAX Metric Translation\n\n## Purpose\n\nTranslate analytical SQL aggregation expressions into equivalent DAX measures.\n\nThe goal is semantic equivalence, not syntactic similarity.\n\nThe generated DAX should:\n\n- Follow Power BI \u002F Tabular best practices\n- Prefer iterator functions when row context is required\n- Use DIVIDE instead of `\u002F NULLIF(...,0)`\n- Fully qualify columns using `'table'[column]`\n- Use CALCULATE where filter context translation is required\n- Preserve aggregation semantics exactly\n- Avoid SQL constructs unsupported in DAX by rewriting logically\n\n---\n\n# SQL Identifier Parsing Rules\n\nThe SQL source may reference columns using any of the following formats:\n\n```sql\ncolumn_name\n```\n\n```sql\ntable.column_name\n```\n\n```sql\ntable.`column name`\n```\n\nThe translator must normalize all forms into valid DAX column references.\n\n---\n\n## Identifier Normalization Rules\n\n| SQL Format | DAX Format |\n|---|---|\n| column_name | 'table'[column_name] |\n| table.column_name | 'table'[column_name] |\n| table.`column name` | 'table'[column name] |\n\n---\n\n## Backtick Handling\n\nSQL backticks must be removed during translation.\n\n### SQL\n```sql\ndim_product.`standard cost`\n```\n\n### DAX\n```dax\n'dim_product'[standard cost]\n```\n\n---\n\n## Unqualified Column Resolution\n\nIf a column is referenced without a table qualifier:\n\n```sql\nSUM(SALES_AMOUNT)\n```\n\nThe translator should:\n\n1. Infer the table from model metadata if available\n2. Prefer the primary fact table in the expression\n3. Fully qualify the final DAX output\n\n### DAX\n```dax\nSUM('fact_sales'[SALES_AMOUNT])\n```\n\n> **Important — bare identifiers inside scalar aggregates must NOT become iterators.**\n>\n> When a SQL aggregate wraps a single bare identifier (e.g. `SUM(ORIGINAL_SALES_AMOUNT)`,\n> `SUM(COST_OF_GOODS_SOLD)`) and that identifier is not declared as a\n> column in the model metadata, the translator must still emit the\n> scalar aggregation form against the default\u002Fowning table:\n>\n> ```dax\n> SUM('fact_returns'[ORIGINAL_SALES_AMOUNT])\n> ```\n>\n> It must **not** fall through to the iterator form:\n>\n> ```dax\n> SUMX('fact_returns', ORIGINAL_SALES_AMOUNT)   -- INVALID\n> ```\n>\n> Iterators (`SUMX`, `AVERAGEX`, etc.) are reserved for cases where the\n> aggregate argument contains arithmetic or references multiple\n> columns. A single bare token is always a scalar aggregation.\n\n---\n\n## Mixed Identifier Formats\n\nExpressions may mix styles.\n\n### SQL\n```sql\nSUM(fact_sales.`sales amount` - DISCOUNT_AMOUNT)\n```\n\n### DAX\n```dax\nSUMX(\n    'fact_sales',\n    'fact_sales'[sales amount] -\n    'fact_sales'[DISCOUNT_AMOUNT]\n)\n```\n\n---\n\n# Core Translation Rules\n\n---\n\n## 1. Aggregate Functions\n\n### SQL\n```sql\nSUM(column)\n```\n\n### DAX\n```dax\nSUM('table'[column])\n```\n\n---\n\n### SQL\n```sql\nAVG(column)\n```\n\n### DAX\n```dax\nAVERAGE('table'[column])\n```\n\n---\n\n### SQL\n```sql\nCOUNT(DISTINCT column)\n```\n\n### DAX\n```dax\nDISTINCTCOUNT('table'[column])\n```\n\n---\n\n## 2. Arithmetic Inside Aggregations\n\nIf arithmetic occurs inside SUM\u002FAVG\u002Fetc., use iterator functions.\n\n### SQL\n```sql\nSUM(price * quantity)\n```\n\n### DAX\n```dax\nSUMX(\n    'table',\n    'table'[price] * 'table'[quantity]\n)\n```\n\n---\n\n### SQL\n```sql\nSUM(revenue - discount)\n```\n\n### DAX\n```dax\nSUMX(\n    'table',\n    'table'[revenue] - 'table'[discount]\n)\n```\n\n---\n\n### SQL\n```sql\nAVG(quantity * cost)\n```\n\n### DAX\n```dax\nAVERAGEX(\n    'table',\n    'table'[quantity] * 'table'[cost]\n)\n```\n\n---\n\n# Iterator Function Rules\n\nUse iterator functions when:\n\n- Multiple columns participate in row-level arithmetic\n- Expressions exist inside aggregate functions\n- Mixed table references occur inside aggregation\n\n| SQL Aggregate | DAX Iterator |\n|---|---|\n| SUM(expr) | SUMX(table, expr) |\n| AVG(expr) | AVERAGEX(table, expr) |\n| MIN(expr) | MINX(table, expr) |\n| MAX(expr) | MAXX(table, expr) |\n\n---\n\n# Safe Division\n\n---\n\n## SQL NULLIF Pattern\n\n### SQL\n```sql\nSUM(sales) \u002F NULLIF(SUM(cost), 0)\n```\n\n### DAX\n```dax\nDIVIDE(\n    SUM('table'[sales]),\n    SUM('table'[cost])\n)\n```\n\n---\n\n## Nested NULLIF\n\n### SQL\n```sql\n365 \u002F NULLIF(metric, 0)\n```\n\n### DAX\n```dax\nDIVIDE(\n    365,\n    [metric]\n)\n```\n\n---\n\n# Percentage Calculations\n\n---\n\n### SQL\n```sql\n(metric \u002F total) * 100\n```\n\n### DAX\n```dax\nDIVIDE(\n    [metric],\n    [total]\n) * 100\n```\n\n---\n\n# ROUND Translation\n\n---\n\n### SQL\n```sql\nROUND(expression, 2)\n```\n\n### DAX\n```dax\nROUND(expression, 2)\n```\n\n---\n\n# DIV0 Translation\n\nDIV0 means divide-by-zero-safe division.\n\n### SQL\n```sql\nDIV0(a, b)\n```\n\n### DAX\n```dax\nDIVIDE(a, b)\n```\n\n---\n\n# Window Function Translation\n\n---\n\n## Rolling Window SUM\n\nTranslate `\u003Cagg>(\u003Cinner>) OVER (ORDER BY \u003Ccol> ROWS BETWEEN N PRECEDING AND CURRENT ROW)`\ninto a `CALCULATE` wrapping the inner aggregation with a `DATESINPERIOD`\nfilter over the ORDER BY column.\n\nRules:\n\n- The window's ORDER BY column becomes the date column passed to\n  `DATESINPERIOD`. It is resolved through the column map and fully\n  qualified as `'table'[column]`.\n- `MAX(\u003Corder_col>)` is used as the anchor date.\n- `ROWS BETWEEN N PRECEDING AND CURRENT ROW` becomes `-N, DAY`. The skill\n  preserves the literal `N` from the SQL (e.g. `89 PRECEDING` → `-89, DAY`)\n  rather than rounding up to the inclusive day count.\n- When the inner expression is itself an aggregate (a non-standard but\n  common Snowflake\u002FBigQuery pattern such as `SUM(SUM(...)) OVER (...)`),\n  the redundant outer aggregate is stripped and only the inner\n  aggregation body is preserved.\n- The inner aggregation body is translated using the normal aggregation\n  rules (including the additive distribution rule that turns\n  `SUM(a - b)` into `(SUM(a) - SUM(b))`).\n\n### SQL\n```sql\nSUM(metric)\nOVER (\n    ORDER BY DATE_KEY\n    ROWS BETWEEN 89 PRECEDING AND CURRENT ROW\n)\n```\n\n### DAX\n```dax\nCALCULATE(\n    [metric],\n    DATESINPERIOD(\n        'dim_date'[date_key],\n        MAX('dim_date'[date_key]),\n        -89,\n        DAY\n    )\n)\n```\n\n---\n\n## Rolling Window over a Nested Aggregate\n\n### SQL\n```sql\nSUM(\n    SUM(EXTENDED_AMOUNT - DISCOUNT_AMOUNT)\n) OVER (\n    ORDER BY DATE_KEY\n    ROWS BETWEEN 89 PRECEDING AND CURRENT ROW\n)\n```\n\n### DAX\n```dax\nCALCULATE(\n    (\n        SUM('fact_sales'[sales_amount])\n        - SUM('fact_sales'[discount_amount])\n    ),\n    DATESINPERIOD(\n        'dim_date'[date_key],\n        MAX('dim_date'[date_key]),\n        -89,\n        DAY\n    )\n)\n```\n\nEquivalently (when distribution is not applied), the inner body may be\nemitted in iterator form:\n\n```dax\nCALCULATE(\n    SUMX(\n        'fact_sales',\n        'fact_sales'[sales_amount] - 'fact_sales'[discount_amount]\n    ),\n    DATESINPERIOD(\n        'dim_date'[date_key],\n        MAX('dim_date'[date_key]),\n        -89,\n        DAY\n    )\n)\n```\n\n---\n\n## Unbounded Window\n\nAn unbounded window (`OVER ()` with no PARTITION BY, ORDER BY, or frame)\nignores the current filter context entirely. Translate it to the\n**iterator form of the aggregate over `ALL(\u003Ctable>)`** — not to\n`CALCULATE(\u003Cagg>, ALL(\u003Ctable>))`.\n\n| SQL aggregate | DAX |\n|---|---|\n| `SUM(col) OVER ()` | `SUMX(ALL('table'), 'table'[col])` |\n| `AVG(col) OVER ()` | `AVERAGEX(ALL('table'), 'table'[col])` |\n| `MIN(col) OVER ()` | `MINX(ALL('table'), 'table'[col])` |\n| `MAX(col) OVER ()` | `MAXX(ALL('table'), 'table'[col])` |\n| `COUNT(col) OVER ()` | `COUNTX(ALL('table'), 'table'[col])` |\n| `COUNT(*) OVER ()` | `COUNTROWS(ALL('table'))` |\n| `COUNT(DISTINCT col) OVER ()` | `CALCULATE(DISTINCTCOUNT('table'[col]), ALL('table'))` |\n\n### SQL\n```sql\nMAX(cutoff) OVER ()\n```\n\n### DAX\n```dax\nMAXX(ALL('table'), 'table'[cutoff])\n```\n\n### SQL\n```sql\nSUM(amount) OVER ()\n```\n\n### DAX\n```dax\nSUMX(ALL('table'), 'table'[amount])\n```\n\n---\n\n# CASE WHEN Translation\n\n---\n\n## Conditional DISTINCTCOUNT\n\n### SQL\n```sql\nCOUNT(DISTINCT CASE\n    WHEN order_count > 1\n    THEN customer_key\nEND)\n```\n\n### DAX\n```dax\nCALCULATE(\n    DISTINCTCOUNT('table'[customer_key]),\n    'table'[order_count] > 1\n)\n```\n\n---\n\n# Multi-Table Arithmetic\n\nWhen expressions reference multiple tables:\n\n- Preserve table qualification\n- Use iterator functions\n- Choose the fact table as the iterator table when possible\n\n---\n\n## Iterator Table Selection from Relationships\n\nWhen an X-function (`SUMX`, `AVERAGEX`, `COUNTX`, `MINX`, `MAXX`) is needed and\nthe inner expression references two tables that participate in a relationship,\nchoose the iterator table as the **\"from\" side** of that relationship\n(typically the many \u002F fact side). Wrap any column reference to the\n**\"to\" side** (typically the one \u002F dimension side) in `RELATED(...)`.\n\nThis rule is independent of the table that the measure is *defined on* — what\nmatters is which table is on the many side of the relationship linking the\ntwo referenced tables.\n\n### Example\n\nGiven a relationship: `fact_sales (Many) → dim_product (One)`\n\n### SQL\n```sql\nSUM(dim_product.standard_cost * fact_sales.quantity_sold)\n```\n\n### DAX\n```dax\nSUMX(\n    'fact_sales',\n    RELATED('dim_product'[standard_cost]) *\n    'fact_sales'[quantity_sold]\n)\n```\n\nEven when this measure is authored on the `dim_product` table, the iterator\ntable is still `fact_sales` because `fact_sales` is the \"from\" side of the\nrelationship.\n\n---\n\n### SQL\n```sql\nSUM(dim_product.standard_cost * fact_sales.quantity)\n```\n\n### DAX\n```dax\nSUMX(\n    'fact_sales',\n    RELATED('dim_product'[standard_cost]) *\n    'fact_sales'[quantity]\n)\n```\n\n---\n\n### SQL\n```sql\nSUM(\n    (EXTENDED_AMOUNT - DISCOUNT_AMOUNT)\n    - (STANDARD_COST * QUANTITY)\n)\n```\n\n### DAX\n```dax\nSUMX(\n    'fact_sales',\n    ('fact_sales'[EXTENDED_AMOUNT] - 'fact_sales'[DISCOUNT_AMOUNT])\n    -\n    (\n        RELATED('dim_product'[STANDARD_COST]) *\n        'fact_sales'[QUANTITY]\n    )\n)\n```\n\n---\n\n# KPI Translation Patterns\n\n---\n\n## Profit\n\n### SQL\n```sql\nSUM(revenue - cost)\n```\n\n### DAX\n```dax\nSUMX(\n    'fact',\n    'fact'[revenue] - 'fact'[cost]\n)\n```\n\n---\n\n## Margin %\n\n### SQL\n```sql\nSUM(profit)\n\u002F\nNULLIF(SUM(revenue), 0)\n```\n\n### DAX\n```dax\nDIVIDE(\n    [Profit],\n    [Revenue]\n)\n```\n\n---\n\n## Return Rate %\n\n### SQL\n```sql\nSUM(return_amount)\n\u002F\nNULLIF(SUM(original_sales_amount), 0)\n```\n\n### DAX\n```dax\nDIVIDE(\n    SUM('fact_returns'[return_amount]),\n    SUM('fact_sales'[original_sales_amount])\n)\n```\n\n---\n\n# Table Qualification Rules\n\n---\n\n## Always Qualify Columns\n\nPreferred:\n```dax\n'fact_sales'[sales_amount]\n```\n\nAvoid:\n```dax\n[sales_amount]\n```\n\n---\n\n# Relationship Translation\n\n---\n\n## SQL Join Semantics\n\nWhen SQL implies dimension lookup:\n\n### SQL\n```sql\ndim_product.standard_cost\n```\n\ninside fact aggregation becomes:\n\n### DAX\n```dax\nRELATED('dim_product'[standard_cost])\n```\n\n---\n\n# Translation Heuristics\n\n---\n\n## Detect Iterator Requirement\n\nUse X-iterators when:\n\n- Expression contains operators inside aggregation\n- More than one column appears inside SUM\u002FAVG\u002Fetc.\n- Arithmetic mixes dimensions and facts\n\n---\n\n## Detect Measure References\n\nNested aggregates inside a window function are flattened — the redundant\nouter aggregate is dropped and only the inner aggregate body is wrapped\nin `CALCULATE`.\n\n### SQL\n```sql\nSUM(SUM(revenue)) OVER (\n    ORDER BY DATE_KEY\n    ROWS BETWEEN 89 PRECEDING AND CURRENT ROW\n)\n```\n\n### DAX\n```dax\nCALCULATE(\n    SUM('fact_sales'[revenue]),\n    DATESINPERIOD(\n        'dim_date'[date_key],\n        MAX('dim_date'[date_key]),\n        -89,\n        DAY\n    )\n)\n```\n\n---\n\n# Common SQL → DAX Mappings\n\n| SQL | DAX |\n|---|---|\n| SUM(col) | SUM(table[col]) |\n| AVG(col) | AVERAGE(table[col]) |\n| COUNT(DISTINCT col) | DISTINCTCOUNT(table[col]) |\n| NULLIF(x,0) | DIVIDE(... ) |\n| ROUND(x,n) | ROUND(x,n) |\n| CASE WHEN | CALCULATE\u002FFILTER |\n| OVER(...) | CALCULATE + time intelligence |\n| SUM(a*b) | SUMX(table,a*b) |\n\n---\n\n# Example Translations\n\n---\n\n## Example 1\n\n### SQL\n```sql\nSUM(store_sales.ss_sales_price * store_sales.ss_quantity)\n```\n\n### DAX\n```dax\nSUMX(\n    'store_sales',\n    'store_sales'[ss_sales_price] *\n    'store_sales'[ss_quantity]\n)\n```\n\n---\n\n## Example 2\n\n### SQL\n```sql\nSUM(ATTRIBUTED_REVENUE)\n\u002F\nNULLIF(SUM(SPEND_AMOUNT), 0)\n```\n\n### DAX\n```dax\nDIVIDE(\n    SUM('fact_marketing'[ATTRIBUTED_REVENUE]),\n    SUM('fact_marketing'[SPEND_AMOUNT])\n)\n```\n\n---\n\n## Example 3\n\n### SQL\n```sql\n(\n  COUNT(DISTINCT CASE\n    WHEN CUSTOMER_ORDER_COUNT > 1\n    THEN CUSTOMER_KEY\n  END)\n  \u002F\n  NULLIF(COUNT(DISTINCT CUSTOMER_KEY), 0)\n) * 100\n```\n\n### DAX\n```dax\nDIVIDE(\n    CALCULATE(\n        DISTINCTCOUNT('customer'[CUSTOMER_KEY]),\n        'customer'[CUSTOMER_ORDER_COUNT] > 1\n    ),\n    DISTINCTCOUNT('customer'[CUSTOMER_KEY])\n) * 100\n```\n\n---\n\n# Output Requirements\n\nGenerated DAX must:\n\n- Be valid Power BI DAX syntax\n- Use proper indentation\n- Use uppercase DAX functions\n- Prefer DIVIDE over `\u002F`\n- Prefer iterators over invalid scalar arithmetic\n- Preserve business semantics exactly\n- Avoid unnecessary CALCULATE wrappers\n- Use RELATED for dimension attribute access\n- Use measures when semantic reuse is implied\n\n---\n\n# Important Semantic Differences\n\nSQL is row-set based.\n\nDAX is filter-context based.\n\nCorrect translation often requires:\n\n- Iterator functions\n- Context transition\n- Relationship navigation\n- Measure decomposition\n\nDo not attempt direct token replacement.\n\n",{"name":5,"description":7},"main","2ddf46109c1209ceffa88149f4631e7b2275f9a1",".claude\u002Fskills\u002Fsql_to_dax",204,"https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fsemantic-link-labs","https:\u002F\u002Fgithub.com\u002Fmicrosoft","https:\u002F\u002Fopensource.microsoft.com","Open source projects and samples from Microsoft","2026-11-13 04:54:42",{"source":8,"skillName":5,"commands":50,"prompt":60,"zipAvailable":61},[51,54,57],{"tool":52,"command":53},"npx","npx skills add microsoft\u002Fsemantic-link-labs --skill sql_to_dax",{"tool":55,"command":56},"bunx","bunx skills add microsoft\u002Fsemantic-link-labs --skill sql_to_dax",{"tool":58,"command":59},"pnpm","pnpm dlx skills add microsoft\u002Fsemantic-link-labs --skill sql_to_dax","安装技能，技能地址：https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fsemantic-link-labs\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Fsql_to_dax\u002FSKILL.md",true,[],[64,91,123,140,154,175],{"skillNo":65,"name":66,"nameZh":6,"description":67,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":68,"categories":69,"keywords":79,"publishedAt":35},"01M699GKE0WWT6YVHBS06KM8AH","write-tests","Guide for writing unit tests. Use this when creating tests to verify Python logic.","https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fsemantic-link-labs\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Fwrite-tests\u002FSKILL.md",[70,73,76],{"categoryNo":71,"categoryName":72,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000008","测试与质量",{"categoryNo":74,"categoryName":75,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000018","写作与文案",{"categoryNo":77,"categoryName":78,"taxonomyType":26,"taxonomyTypeDesc":27},"01JBSEEDCAT000000000000036","内容创作者",[80,81,82,34,83,84,85,86,87,88,33,89,90],"tests","write","write tests","unit","when","logic","use","writing","python","verify","unit tests",{"skillNo":92,"name":93,"nameZh":6,"description":94,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":95,"categories":96,"keywords":106,"publishedAt":35},"01M699GGVJHWCJNW68P08VWCMA","ui-styling","Guide for the visual style, structure, and shared building blocks used by Semantic Link Labs interactive UI tools (HTML widgets and anywidget-based widgets). Use this when adding a new interactive UI, modifying an existing one, or adding shared visual components.","https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fsemantic-link-labs\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Fui-styling\u002FSKILL.md",[97,100,103],{"categoryNo":98,"categoryName":99,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000009","前端开发",{"categoryNo":101,"categoryName":102,"taxonomyType":21,"taxonomyTypeDesc":22},"01JBSEEDCAT000000000000015","视觉设计",{"categoryNo":104,"categoryName":105,"taxonomyType":26,"taxonomyTypeDesc":27},"01JBSEEDCAT000000000000032","设计师",[107,108,109,110,86,33,111,112,113,114,115,116,117,118,119,120,121,34,122,84],"ui styling","ui","styling","html","interactive","Building Blocks","Tools","shared","components","structure","visual","widgets","style","visual style","blocks","Existing",{"skillNo":124,"name":125,"nameZh":6,"description":126,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":127,"categories":128,"keywords":129,"publishedAt":35},"01M699GEZ3YRK8PCPVZF4NEEWF","tom-operations","Guide for working with the TOM (Tabular Object Model) wrapper. Use this when modifying semantic models programmatically.","https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fsemantic-link-labs\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Ftom-operations\u002FSKILL.md",[],[130,131,84,132,34,133,134,135,136,86,33,137,138,139],"operations","tom operations","tabular","object","semantic","models","Wrapper","model","semantic models","with",{"skillNo":141,"name":142,"nameZh":6,"description":143,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":144,"categories":145,"keywords":146,"publishedAt":35},"01M699GAY10VVK66E4QGT94A93","run-tests","Guide for running pytest tests locally. Use this when running tests to verify code changes.","https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fsemantic-link-labs\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Frun-tests\u002FSKILL.md",[],[80,147,148,149,150,84,34,151,86,33,89,152,153],"run","run tests","changes","running","pytest","running tests","code",{"skillNo":155,"name":156,"nameZh":6,"description":157,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":158,"categories":159,"keywords":164,"publishedAt":35},"01M699G8V718EXYE8BR7NX734G","rest-api-patterns","Guide for implementing REST API wrapper functions. Use this when adding new API wrappers or troubleshooting API calls.","https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fsemantic-link-labs\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Frest-api-patterns\u002FSKILL.md",[160,161],{"categoryNo":19,"categoryName":20,"taxonomyType":21,"taxonomyTypeDesc":22},{"categoryNo":162,"categoryName":163,"taxonomyType":26,"taxonomyTypeDesc":27},"01JBSEEDCAT000000000000030","开发工程师",[165,166,167,168,169,170,171,172,173,136,86,33,174,34,84],"rest api","patterns","api","api patterns","REST","rest api patterns","implementing","calls","functions","troubleshooting",{"skillNo":176,"name":177,"nameZh":6,"description":178,"descriptionZh":6,"repositoryFullName":8,"ownerLogin":9,"ownerAvatarUrl":10,"ownerAccountNo":11,"ownerDisplayName":12,"repositoryNo":13,"starsCount":14,"licenseSpdxId":15,"licenseFileUrl":6,"sourceHtmlUrl":179,"categories":180,"keywords":181,"publishedAt":35},"01M699G6XY1WTG7ESA1Y7YYCKT","query-builder-dax","Guide for the DAX query shape produced by the Query Builder in the DAX test widget. Use this when modifying how the Query Builder generates DAX, or when generating an EVALUATE query from columns, measures, filters and sorting.","https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fsemantic-link-labs\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Fquery-builder-dax\u002FSKILL.md",[],[29,182,183,184,185,186,34,84,187,188,189,190,191,192,86,33,193],"query builder","builder","query builder dax","query","test","evaluate","sorting","columns","widget","filters","shape","how"]