[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"skill:01M699GCWWBZM2XFNW5PTGRFJP":3,"skill-same:01M699GCWWBZM2XFNW5PTGRFJP":65},{"skillNo":4,"name":5,"nameZh":6,"description":7,"descriptionZh":8,"repositoryFullName":9,"ownerLogin":10,"ownerAvatarUrl":11,"ownerAccountNo":12,"ownerDisplayName":13,"repositoryNo":14,"starsCount":15,"licenseSpdxId":16,"licenseFileUrl":17,"sourceHtmlUrl":18,"categories":19,"keywords":30,"publishedAt":37,"linkOnly":38,"markdownContent":39,"markdownBody":40,"frontMatter":41,"refName":42,"commitSha":43,"skillDirectory":44,"forksCount":45,"repositoryHtmlUrl":46,"repositoryHomepageUrl":17,"ownerHtmlUrl":47,"ownerBlogUrl":48,"ownerDescription":49,"updatedAt":50,"install":51,"resources":64},"01M699GCWWBZM2XFNW5PTGRFJP","sql_to_dax","SQL 转 DAX 度量值","Guide for translating SQL aggregation expressions into DAX measures.","将 SQL 聚合表达式转换为 DAX 度量值的指南。","microsoft\u002Fsemantic-link-labs","microsoft","https:\u002F\u002Favatars.githubusercontent.com\u002Fu\u002F6154722?v=4","01M698VYDXZ6JW5WH3CRV650R8","Microsoft","01M699FKMT63C17BFNH8WHZN2K",600,"MIT",null,"https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fsemantic-link-labs\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Fsql_to_dax\u002FSKILL.md",[20,25],{"categoryNo":21,"categoryName":22,"taxonomyType":23,"taxonomyTypeDesc":24},"01M8BQGD4CMBM6B4PWSM1NXGT9","数据分析与报告","SCENARIO","使用场景",{"categoryNo":26,"categoryName":27,"taxonomyType":28,"taxonomyTypeDesc":29},"01M8BQGD5CX980XNXCJKQF70BY","数据分析师","PROFESSION","适用职业",[31,32,33,34,35,36],"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":9,"skillName":5,"commands":52,"prompt":62,"zipAvailable":63},[53,56,59],{"tool":54,"command":55},"npx","npx skills add microsoft\u002Fsemantic-link-labs --skill sql_to_dax",{"tool":57,"command":58},"bunx","bunx skills add microsoft\u002Fsemantic-link-labs --skill sql_to_dax",{"tool":60,"command":61},"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,[],[66,92,123,146,164,187],{"skillNo":67,"name":68,"nameZh":69,"description":70,"descriptionZh":71,"repositoryFullName":9,"ownerLogin":10,"ownerAvatarUrl":11,"ownerAccountNo":12,"ownerDisplayName":13,"repositoryNo":14,"starsCount":15,"licenseSpdxId":16,"licenseFileUrl":17,"sourceHtmlUrl":72,"categories":73,"keywords":80,"publishedAt":37},"01M699GKE0WWT6YVHBS06KM8AH","write-tests","Python 单元测试编写","Guide for writing unit tests. Use this when creating tests to verify Python logic.","编写单元测试指南。在创建测试以验证 Python 逻辑时使用。","https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fsemantic-link-labs\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Fwrite-tests\u002FSKILL.md",[74,77],{"categoryNo":75,"categoryName":76,"taxonomyType":28,"taxonomyTypeDesc":29},"01M8BQGD5WG86P4AQ5GPSSKBFR","测试工程师",{"categoryNo":78,"categoryName":79,"taxonomyType":23,"taxonomyTypeDesc":24},"01M8BQJH394GT47X57A9Q8G7HX","测试与质量保障",[81,82,83,36,84,85,86,87,88,89,35,90,91],"tests","write","write tests","unit","when","logic","use","writing","python","verify","unit tests",{"skillNo":93,"name":94,"nameZh":95,"description":96,"descriptionZh":97,"repositoryFullName":9,"ownerLogin":10,"ownerAvatarUrl":11,"ownerAccountNo":12,"ownerDisplayName":13,"repositoryNo":14,"starsCount":15,"licenseSpdxId":16,"licenseFileUrl":17,"sourceHtmlUrl":98,"categories":99,"keywords":106,"publishedAt":37},"01M699GGVJHWCJNW68P08VWCMA","ui-styling","UI 视觉样式设计","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.","关于 Semantic Link Labs 交互式 UI 工具（HTML 小部件和基于 anywidget 的小部件）所使用的视觉风格、结构和共享构建块的指南。在添加新的交互式 UI、修改现有 UI 或添加共享视觉组件时使用此技能。","https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fsemantic-link-labs\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Fui-styling\u002FSKILL.md",[100,103],{"categoryNo":101,"categoryName":102,"taxonomyType":28,"taxonomyTypeDesc":29},"01M8BQ6TQZN6R52WBJ9H8NRMQM","软件开发工程师",{"categoryNo":104,"categoryName":105,"taxonomyType":23,"taxonomyTypeDesc":24},"01M8BRASNPVNJVQQJ4KZX8XM5D","前端开发与界面实现",[107,108,109,110,87,35,111,112,113,114,115,116,117,118,119,120,121,36,122,85],"ui styling","ui","styling","html","interactive","Building Blocks","Tools","shared","components","structure","visual","widgets","style","visual style","blocks","Existing",{"skillNo":124,"name":125,"nameZh":126,"description":127,"descriptionZh":128,"repositoryFullName":9,"ownerLogin":10,"ownerAvatarUrl":11,"ownerAccountNo":12,"ownerDisplayName":13,"repositoryNo":14,"starsCount":15,"licenseSpdxId":16,"licenseFileUrl":17,"sourceHtmlUrl":129,"categories":130,"keywords":135,"publishedAt":37},"01M699GEZ3YRK8PCPVZF4NEEWF","tom-operations","TOM 表格对象模型操作","Guide for working with the TOM (Tabular Object Model) wrapper. Use this when modifying semantic models programmatically.","关于使用 TOM（表格对象模型，Tabular Object Model）包装器的指南。当以编程方式修改语义模型时使用此技能。","https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fsemantic-link-labs\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Ftom-operations\u002FSKILL.md",[131,132],{"categoryNo":21,"categoryName":22,"taxonomyType":23,"taxonomyTypeDesc":24},{"categoryNo":133,"categoryName":134,"taxonomyType":28,"taxonomyTypeDesc":29},"01M8BTGTEHKD89NSZ5CT4Y5AGV","数据工程师",[136,137,85,138,36,139,140,141,142,87,35,143,144,145],"operations","tom operations","tabular","object","semantic","models","Wrapper","model","semantic models","with",{"skillNo":147,"name":148,"nameZh":149,"description":150,"descriptionZh":151,"repositoryFullName":9,"ownerLogin":10,"ownerAvatarUrl":11,"ownerAccountNo":12,"ownerDisplayName":13,"repositoryNo":14,"starsCount":15,"licenseSpdxId":16,"licenseFileUrl":17,"sourceHtmlUrl":152,"categories":153,"keywords":156,"publishedAt":37},"01M699GAY10VVK66E4QGT94A93","run-tests","pytest 测试运行","Guide for running pytest tests locally. Use this when running tests to verify code changes.","本地运行 pytest 测试的指南。在运行测试以验证代码更改时使用。","https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fsemantic-link-labs\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Frun-tests\u002FSKILL.md",[154,155],{"categoryNo":75,"categoryName":76,"taxonomyType":28,"taxonomyTypeDesc":29},{"categoryNo":78,"categoryName":79,"taxonomyType":23,"taxonomyTypeDesc":24},[81,157,158,159,160,85,36,161,87,35,90,162,163],"run","run tests","changes","running","pytest","running tests","code",{"skillNo":165,"name":166,"nameZh":167,"description":168,"descriptionZh":169,"repositoryFullName":9,"ownerLogin":10,"ownerAvatarUrl":11,"ownerAccountNo":12,"ownerDisplayName":13,"repositoryNo":14,"starsCount":15,"licenseSpdxId":16,"licenseFileUrl":17,"sourceHtmlUrl":170,"categories":171,"keywords":176,"publishedAt":37},"01M699G8V718EXYE8BR7NX734G","rest-api-patterns","REST API 封装实现","Guide for implementing REST API wrapper functions. Use this when adding new API wrappers or troubleshooting API calls.","实现 REST API 包装函数的指南。在添加新的 API 包装器或排查 API 调用问题时使用。","https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fsemantic-link-labs\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Frest-api-patterns\u002FSKILL.md",[172,173],{"categoryNo":101,"categoryName":102,"taxonomyType":28,"taxonomyTypeDesc":29},{"categoryNo":174,"categoryName":175,"taxonomyType":23,"taxonomyTypeDesc":24},"01M8BT7QVANT4709358VEJK53E","代码实现",[177,178,179,180,181,182,183,184,185,142,87,35,186,36,85],"rest api","patterns","api","api patterns","REST","rest api patterns","implementing","calls","functions","troubleshooting",{"skillNo":188,"name":189,"nameZh":190,"description":191,"descriptionZh":192,"repositoryFullName":9,"ownerLogin":10,"ownerAvatarUrl":11,"ownerAccountNo":12,"ownerDisplayName":13,"repositoryNo":14,"starsCount":15,"licenseSpdxId":16,"licenseFileUrl":17,"sourceHtmlUrl":193,"categories":194,"keywords":197,"publishedAt":37},"01M699G6XY1WTG7ESA1Y7YYCKT","query-builder-dax","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.","关于 DAX 测试小部件中查询构建器生成的 DAX 查询形状的指南。当需要修改查询构建器生成 DAX 的方式，或从列、度量值、筛选器和排序生成 EVALUATE 查询时使用。","https:\u002F\u002Fgithub.com\u002Fmicrosoft\u002Fsemantic-link-labs\u002Fblob\u002Fmain\u002F.claude\u002Fskills\u002Fquery-builder-dax\u002FSKILL.md",[195,196],{"categoryNo":21,"categoryName":22,"taxonomyType":23,"taxonomyTypeDesc":24},{"categoryNo":26,"categoryName":27,"taxonomyType":28,"taxonomyTypeDesc":29},[31,198,199,200,201,202,36,85,203,204,205,206,207,208,87,35,209],"query builder","builder","query builder dax","query","test","evaluate","sorting","columns","widget","filters","shape","how"]