DAX 查询设计
query-design
将 DAX 数据获取与 TypeScript 呈现分离。指导在聚合、总计行、FORMAT()、SELECTCOLUMNS、BLANK 处理、固定筛选器、多粒度查询和格式字符串等场景下,何时使用 DAX、TypeScript 或 Vega-Lite。
- License 许可
- MIT
- 最近更新
- 2026-11-14
- Stars
- ★ 30
- Forks
- 14
01 前言 Front Matter
- name
- query-design
- description
- Separates DAX data-fetching from TypeScript presentation. Guides when to use DAX vs. TypeScript vs. Vega-Lite for aggregation, total rows, FORMAT(), SELECTCOLUMNS, BLANK handling, fixed filters, multi-grain queries, and format strings.
02 资源文件 3
- 参考文档references/anti-patterns.md10.6 KB
- 参考文档references/format-strings.md2.9 KB
- 参考文档references/multi-grain-patterns.md9.6 KB
03 SKILL.md 正文
Query Design — Separation of Data and Presentation
DAX computes and fetches data. TypeScript shapes it for display. VegaVisual and DataGrid render it.
Aggregate in DAX to the visual's grain — never fetch lower-grain rows to roll up client-side. When a visual layout changes, only the TypeScript or spec layer should change — not the DAX query.
Responsibility Matrix
| Concern | Owner |
|---|---|
| Semantic measures (SUM, DISTINCTCOUNT, etc.) | DAX |
| Fixed model/page filters | DAX |
| User-controlled visual filters | visual-interactivity |
| Grouping grain (SUMMARIZECOLUMNS) | DAX |
| Time intelligence (YTD, YoY) | DAX |
| TopN / payload reduction | DAX |
| Deterministic row ordering (ORDER BY) | DAX (for debugging — not presentation sort) |
| Partitioning a flagged result table | TypeScript |
| Coordinating separate query results | TypeScript (one hook call per result table) |
| Server-provided grand totals (preferred, supports all aggregation types) | DAX + TypeScript row partitioning |
| DataGrid-computed grand totals | DataGrid (alternative only for additive sum values or a count of fetched leaf rows) |
| Filling dimension gaps | TypeScript (stitch dimension list into sparse results) |
| Reshaping (pivot, unpivot) | TypeScript |
| Column display names | columnMetadata in factory file |
| Vega-Lite / DataGrid field identifiers | columnMetadata.name (bracket-free alias) |
| Number/date formatting | columnMetadata.format / Vega-Lite spec |
| User-facing sort order | TypeScript / Vega-Lite sort / DataGrid sort |
| Decorative labels, icons | DataGrid cellRenderer or Vega-Lite condition |
| Axis titles, legends, color encoding | Vega-Lite spec |
Rules
Must
- Aggregate in DAX to the visual's grain — never fetch lower-grain rows just to roll them up to that grain in TypeScript
- One
EVALUATEper.daxfile; useROLLUPADDISSUBTOTALwhen DataGrid body and grand-total grains share one result table ORDER BYin DAX for stable, diffable results — not presentation sort- Same filters/measures across related split-grain queries to prevent drift
Prefer
SUMMARIZECOLUMNSfor grouped aggregation — it also drops BLANK-measure rows, keeping payloads small- DAX's natural column names (
'Table'[Column],[Measure]) ascolumnMetadatakeys, mapped to a bracket-freenameand a human-readabledisplayName - Raw typed values from DAX — format via
columnMetadata.formator Vega-Lite, neverFORMAT() - Model-defined format strings (from
INFO.VIEW.MEASURES()) over invented ones - Multiple lightweight queries for independently shaped datasets; one flagged rollup query for a DataGrid body and grand total
- User-facing sort in TypeScript / Vega-Lite / DataGrid — never re-query for sort
Avoid
SELECTCOLUMNSsolely for renaming — usecolumnMetadata(name+displayName) instead- Reusing the raw DAX column name as
columnMetadata.name—[].make the visual readundefined UNIONto mix body and total grains — useROLLUPADDISSUBTOTALand partition its flagged rowsFORMAT()in DAX — converts to text, breaks sorting and charting- Converting BLANK to
0/""/"N/A"in DAX — causes result-set explosion CONCATENATEX,UNICHAR, emoji prefixes — decorative text belongs incellRendereror Vega-Lite- Fetching all members of high-cardinality dimensions just to fill gaps
Decision Flowchart
Need to add something to the query result?
|-- Calculation / aggregation / fixed scope filter?
| -> DAX (measures, CALCULATE, SUMMARIZECOLUMNS)
|-- Interactive filter the user controls?
| -> Use the visual-interactivity skill
|-- Adding a DataGrid grand total?
| -> DAX rollup query: split body and total rows with toRollupDataTables
| and always pass the returned grandTotalTable via grandTotals.data
| -> DataGrid-computed sum/count: pass grandTotals without data and set defaultAggregation
|-- Merging datasets or adding other synthetic rows?
| -> Charts: pass multiple DataTables to VegaVisual, layer in spec
| -> Grids: append rows in TypeScript, style via cellRenderer
|-- Renaming a column for display?
| -> columnMetadata in the factory file (displayName)
|-- Formatting, labeling, or encoding?
| -> Vega-Lite spec or DataGrid cellRenderer
|-- Decorating values (icons, status badges, null placeholders)?
| -> DataGrid cellRenderer or Vega-Lite condition encoding
|-- Not sure?
-> Does it change what the data *means* (filter, measure, grain)? -> DAX
Does it change only how data is *rendered* (labels, icons, layout)? -> TypeScript / Vega-Lite spec / DataGrid cellRenderer
Still unclear? -> Read the relevant reference above
Reference Materials
Read these when working on a specific topic:
- Anti-patterns and corrections — Open when reviewing a query that uses
UNIONfor totals,FORMAT(),SELECTCOLUMNSfor renaming,CONCATENATEX/emoji decoration, BLANK-to-0conversion, orGENERATE/CROSSJOINfor gap-filling. - Multi-grain patterns — Open when a single visualization needs data at multiple grains (e.g., bars + reference line, region detail + total row, monthly trend + YTD), to choose between one flagged rollup result and separate queries while keeping related factories, hooks, and
DataTables aligned. - Format strings — Open when picking a
columnMetadata.formatvalue, when a measure has a dynamic format string, or when formatting needs to flow into a Vega-Lite axis.
Integration with Sibling Skills
- schema-query — Query exact tables, columns, measures, and relationships before writing queries.
- dax-authoring — DAX syntax, query patterns, and testing workflow. Apply this skill's principles when deciding what DAX should compute.
- visuals — Vega-Lite specs and DataGrid configuration. Push formatting and labels into specs, not DAX.
- visual-interactivity — Visual selections, cross-filtering, cross-highlighting, filter-aware grid totals, and runtime query parameters.
04 同仓库更多 Skills
数据可视化组件开发
visuals
当用户希望将图表、图形、KPI 卡片、数据网格、切片器或其他数据可视化表示整合到其项目中时使用。使用 VegaVisual、KpiCard、DataGrid 和内置切片器组件,利用共享的 DataTable 输入、格式化和主题。涵盖 VisualContainer 为图表、KPI 卡片和网格默认提供的框架,以及用于分层叠加和参考线的命名多表数据输入。
可视化交互联动实现
visual-interactivity
实现可视化选择及协调一致的可视化行为。涵盖 Vega-Lite 原生选择、VegaVisual 与 DataGrid 交互事件、交叉筛选、交叉高亮、感知筛选的网格总计,以及用于运行时可视化状态的参数化查询工厂。
语义模型数据读取
semantic-model-data
应用如何在运行时读取语义模型数据:useSemanticModelQuery、其规范化结果与错误结构、错误类别与数据溢出、行边界限制,以及应用级查询缓存。
语义模型 DAX 查询
schema-query
基于缓存的语义模型目录、已发布的度量值、关系以及显式筛选值来开展 DAX 工作。
DAX 查询编写与测试
dax-authoring
针对 Power BI 语义模型编写和测试 DAX 查询。涵盖 DAX 语法规则、查询模式、时间智能,以及使用 `npx fabric-app-semantic-model-tools run-dax-query --dax <file> --alias <name>` 的迭代测试工作流。
应用界面与视觉设计
app-design
在构建或修改应用布局、创建 UI 组件或做出任何视觉设计决策时使用。确保一致性、可访问性,以及精致、独特的应用。