使用 Excel 中的值过滤 Power Query

问题描述 投票:0回答:1

我正在使用强力查询来过滤数据库,我想根据Excel中的单元格值进行过滤。当单元格中没有值时,我不想应用过滤器。

我希望过滤器起作用的列的名称是“过滤行”步骤之后应用的“国家/地区名称”

当前代码:

let
Source = OData.Feed("https://data.woodmac.com/query/upstream_weekly/imp-met/odata", null, [Implementation="2.0"]),
field_company_net_annual_production_kbd_table = Source{[Name="field_company_net_annual_production_kbd",Signature="table"]}[Data],
#"Filtered Rows" = Table.SelectRows(field_company_net_annual_production_kbd_table, each ([year] = 2024) and ([field_is_top_level] = "Y")),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"id_field_company_metrics", "id_field", "id_field_group", "field_is_top_level", "field_is_discovery", "id_country", "id_company", "id_subsidiary", "subsidiary_name", "field_interest", "id_metric", "id_unit", "id_year", "year_actual_forecast", "year_is_current", "id_produced_remaining", "produced_remaining", "id_commercial_technical", "commercial_technical", "commercial_technical_breakdown", "prms_commercial_subcommercial", "prms_breakdown", "id_product", "liquid_gas", "id_category", "category", "category_breakdown", "id_price_deck", "price_deck", "price_deck_long_term_brent", "id_real_nominal", "real_nominal", "id_discount", "discount_rate", "metric_order", "id_theme", "unit_metric_imperial", "metric_is_volume", "metric_can_be_summed", "field_interest_date_start", "field_interest_date_end", "field_interest_pro_rated", "interest_change_type", "interest_change_delta", "entry_character", "entry_date", "company_subsid_via", "interest_is_current", "current_interest", "asset_is_currently_owned", "field_interest_is_direct", "company_is_top_level", "company_primary_interest", "field_interest_is_latest", "company_to_subsidiary_category", "company_subsid_interest", "company_to_field_chain", "company_peer_group", "company_colour", "company_is_dummy", "iso_3166_1_alpha_3_code", "id_super_region", "super_region", "id_region", "region", "id_sector", "sector_name", "sector_country", "country_region", "is_arctic", "country_opec_oecd", "water_depth_category_tags", "id_basin", "basin_name"}),
#"Removed Columns1" = Table.RemoveColumns(#"Removed Columns",{"id_field_parent", "field_parent", "field_production_area", "field_operator", "field_name_unique", "field_is_parent", "field_group_level", "onshore_offshore_tags", "field_oil_gas", "field_hydrocarbon_type", "field_conventional_unconv", "field_unconventional_type", "field_onshore_offshore_break", "field_primary_resource_theme", "field_primary_resource_group", "field_taxation", "field_fiscal_regime", "field_status", "field_prms_breakdown", "field_maturity", "field_development_type", "field_project_type", "field_is_lng_supply", "field_date_discovery", "field_year_discovery", "field_date_feed", "field_date_fid", "field_year_capex_first", "field_date_production_start", "field_year_production_start", "field_year_production_end", "field_size", "field_awarded_as_dro", "field_recovery_stage", "field_drive_mechanism_primary", "field_drive_mechanism_second", "field_drive_mechanism_tertiary", "field_class_oil_gravity", "field_class_oil_sulphur", "field_class_acid_gas", "field_class_sour_gas", "id_play", "play_name", "id_sub_play", "sub_play_name", "sub_play_name_short", "play_conventional_unconv", "play_resource_type_primary", "play_resource_type_all", "play_hydrocarbon_primary", "play_hydrocarbon_all", "play_development_status", "play_development_type", "play_well_slant", "play_model_type_well_only", "play_lithology_primary", "play_grade_environmental_reg", "play_grade_fiscal_terms", "play_grade_market_gas", "play_grade_price_gas", "play_grade_infrastructure", "play_grade_land_access", "play_grade_market_liq", "play_grade_resource_upside", "play_grade_supply_chain", "play_grade_technology_upside", "play_grade_water_access", "field_centroid_x", "field_centroid_y", "id_portal_area", "company_category", "company_state_owned", "company_is_defunct", "company_date_founded", "company_legal_entity_type", "hq_city", "hq_country", "hq_region", "hq_super_region", "id_country_hq", "domestic_or_international", "company_is_stock_listed", "stock_exchange_name", "stock_ticker_symbol", "id_country_stock", "id_history"})
in
#"Removed Columns1"

我已经能够按 Excel 单元格中的值进行过滤,但当我不向单元格中输入任何值时,代码不起作用

excel powerquery
1个回答
0
投票

在Excel中创建范围名称,此处aaa

将该值读入 powerquery 并将其分配给变量名称

NameValue= Excel.CurrentWorkbook(){[Name="aaa"]}[Content]{0}[Column1],

使用该变量名称作为新过滤步骤的一部分

#"Filtered Rows2" = Table.SelectRows(#"Filtered Rows", each [country_name] = NameValue),

将所有内容放在一起

let
Source = OData.Feed("https://data.woodmac.com/query/upstream_weekly/imp-met/odata", null, [Implementation="2.0"]),
field_company_net_annual_production_kbd_table = Source{[Name="field_company_net_annual_production_kbd",Signature="table"]}[Data],
NameValue= Excel.CurrentWorkbook(){[Name="aaa"]}[Content]{0}[Column1],
#"Filtered Rows" = Table.SelectRows(field_company_net_annual_production_kbd_table, each ([year] = 2024) and ([field_is_top_level] = "Y")),
#"Filtered Rows2" = Table.SelectRows(#"Filtered Rows", each [country_name] = NameValue),
#"Removed Columns" = Table.RemoveColumns(#"Filtered Rows2",{"id_field_company_metrics", "id_field", "id_field_group", "field_is_top_level", "field_is_discovery", "id_country", "id_company", "id_subsidiary", "subsidiary_name", "field_interest", "id_metric", "id_unit", "id_year", "year_actual_forecast", "year_is_current", "id_produced_remaining", "produced_remaining", "id_commercial_technical", "commercial_technical", "commercial_technical_breakdown", "prms_commercial_subcommercial", "prms_breakdown", "id_product", "liquid_gas", "id_category", "category", "category_breakdown", "id_price_deck", "price_deck", "price_deck_long_term_brent", "id_real_nominal", "real_nominal", "id_discount", "discount_rate", "metric_order", "id_theme", "unit_metric_imperial", "metric_is_volume", "metric_can_be_summed", "field_interest_date_start", "field_interest_date_end", "field_interest_pro_rated", "interest_change_type", "interest_change_delta", "entry_character", "entry_date", "company_subsid_via", "interest_is_current", "current_interest", "asset_is_currently_owned", "field_interest_is_direct", "company_is_top_level", "company_primary_interest", "field_interest_is_latest", "company_to_subsidiary_category", "company_subsid_interest", "company_to_field_chain", "company_peer_group", "company_colour", "company_is_dummy", "iso_3166_1_alpha_3_code", "id_super_region", "super_region", "id_region", "region", "id_sector", "sector_name", "sector_country", "country_region", "is_arctic", "country_opec_oecd", "water_depth_category_tags", "id_basin", "basin_name"}),
#"Removed Columns1" = Table.RemoveColumns(#"Removed Columns",{"id_field_parent", "field_parent", "field_production_area", "field_operator", "field_name_unique", "field_is_parent", "field_group_level", "onshore_offshore_tags", "field_oil_gas", "field_hydrocarbon_type", "field_conventional_unconv", "field_unconventional_type", "field_onshore_offshore_break", "field_primary_resource_theme", "field_primary_resource_group", "field_taxation", "field_fiscal_regime", "field_status", "field_prms_breakdown", "field_maturity", "field_development_type", "field_project_type", "field_is_lng_supply", "field_date_discovery", "field_year_discovery", "field_date_feed", "field_date_fid", "field_year_capex_first", "field_date_production_start", "field_year_production_start", "field_year_production_end", "field_size", "field_awarded_as_dro", "field_recovery_stage", "field_drive_mechanism_primary", "field_drive_mechanism_second", "field_drive_mechanism_tertiary", "field_class_oil_gravity", "field_class_oil_sulphur", "field_class_acid_gas", "field_class_sour_gas", "id_play", "play_name", "id_sub_play", "sub_play_name", "sub_play_name_short", "play_conventional_unconv", "play_resource_type_primary", "play_resource_type_all", "play_hydrocarbon_primary", "play_hydrocarbon_all", "play_development_status", "play_development_type", "play_well_slant", "play_model_type_well_only", "play_lithology_primary", "play_grade_environmental_reg", "play_grade_fiscal_terms", "play_grade_market_gas", "play_grade_price_gas", "play_grade_infrastructure", "play_grade_land_access", "play_grade_market_liq", "play_grade_resource_upside", "play_grade_supply_chain", "play_grade_technology_upside", "play_grade_water_access", "field_centroid_x", "field_centroid_y", "id_portal_area", "company_category", "company_state_owned", "company_is_defunct", "company_date_founded", "company_legal_entity_type", "hq_city", "hq_country", "hq_region", "hq_super_region", "id_country_hq", "domestic_or_international", "company_is_stock_listed", "stock_exchange_name", "stock_ticker_symbol", "id_country_stock", "id_history"})
in "Removed Columns1"

您可能只想保留某些列,而不是删除多余的列。代码更少

© www.soinside.com 2019 - 2024. All rights reserved.