---
title: "Lusid.PortfolioGroup.Writer"
slug: "lusidportfoliogroupwriter"
updated: 2024-08-21T12:52:17Z
published: 2024-08-21T12:52:17Z
canonical: "support.lusid.com/lusidportfoliogroupwriter"
---

> ## Documentation Index
> Fetch the complete documentation index at: https://support.lusid.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Lusid.PortfolioGroup.Writer

| **Type** | **Read/write** | **Author** | **Availability** |
| --- | --- | --- | --- |
| [Data provider](/v1/docs/what-are-a-data-provider-and-a-direct-provider) | Write | Finbourne | Provided with LUSID |

Providing you have sufficient [access control permissions](/v1/docs/creating-policies-to-control-access-to-providers-for-different-luminesce-end-users), the `Lusid.PortfolioGroup.Writer` provider enables you to write a [Luminesce SQL query](/v1/docs/understanding-the-luminesce-sql-query-syntax) that either creates, updates or deletes [portfolio groups](/v1/docs/what-is-a-portfolio-group) in LUSID, and also adds and removes portfolios and sub-groups.

> **Note**: By default, `Lusid.PortfolioGroup.Writer` cannot add properties to portfolio groups. To do this, you must first configure `Lusid.PortfolioGroup.Writer` to 'inline' properties. [See how to do this](/v1/docs/how-do-i-configure-luminesce-entity-providers-to-readwrite-properties).

You must construct a valid table of data to write, one portfolio group or portfolio per record, depending on the operation. `Lusid.PortfolioGroup.Writer` lists the fields (columns) available to populate with values for each record, and has a set of parameters to help you construct a valid table.

Your query can use the `WriteAction` field to perform one of the following operations:

- Upsert a portfolio group; that is, create a portfolio group if it does not exist, and update certain fields if it does. This is the default operation if you omit `WriteAction`.
- Delete a portfolio group.
- Add a portfolio or a sub-group to a portfolio group.
- Remove a portfolio or a sub-group from a portfolio group.

**See also:** [Lusid.PortfolioGroup](/v1/docs/lusidportfoliogroup)

## Basic usage

```sql
@table_of_data = <select-statement>;
select * from Lusid.PortfolioGroup.Writer where toWrite = @table_of_data;
```

## Query parameters

`Lusid.PortfolioGroup.Writer` has parameters that help you construct a valid table of data to write.

> **Note:** The `toWrite` parameter is mandatory and used to actually write the table of data into LUSID.

To list available parameters, their data types, default values, and an explanation for each, run the following query using a [suitable tool](/v1/docs/what-tools-are-available-to-write-luminesce-queries):

```sql
select FieldName, DataType, ParamDefaultValue, Description from Sys.Field where TableName = 'Lusid.PortfolioGroup.Writer' and FieldType = 'Parameter';
```

## Data fields

`Lusid.PortfolioGroup.Writer` lists the fields you can populate in your table of data to write.

Depending on the operation you want to perform, the following fields are mandatory to include in the table of data:

| **Operation** | **Specify using...** | **Mandatory fields** | **Notes** |
| --- | --- | --- | --- |
| Upsert | `'Upsert' as WriteAction` (or omit) | `TopGroupScope` `TopGroupCode` `TopGroupName` `TopGroupDescription` | The `TopGroupName` and `TopGroupDescription` of a portfolio group are updated if `TopGroupScope` and `TopGroupCode` exist, and a new portfolio group is created if not. |
| Insert | `'Insert' as WriteAction` |  |
| Update | `'Update' as WriteAction` | You can only update `TopGroupName` and `TopGroupDescription` for an existing portfolio group. |
| Delete | `'Delete' as WriteAction` | `TopGroupScope` `TopGroupCode` |  |
| Add | `'Add' as WriteAction` | `TopGroupScope` `TopGroupCode` `PortfolioScope` `PortfolioCode` `BottomGroupScope` `BottomGroupCode` | `TopGroupScope` and `TopGroupCode` are always mandatory, to identify the portfolio group to operate on. `PortfolioScope` and `PortfolioCode` are mandatory to add or remove a portfolio. `BottomGroupCode` and `BottomGroupScope` are mandatory to add or remove a sub-group. |
| Remove | `'Remove' as WriteAction` |

To list all available fields, their data types, whether fields are considered 'main', and an explanation for each, run the following query using a [suitable tool](/v1/docs/what-tools-are-available-to-write-luminesce-queries):

```sql
select FieldName, DataType, IsMain, IsPrimaryKey, SampleValues, Description from Sys.Field where TableName = 'Lusid.PortfolioGroup.Writer' and FieldType = 'Column';
```

## Write errors

We recommend examining the results of every write query using one or more of the `WriteError`, `WriteErrorCode` and `WriteErrorDetail` fields.

For each record in the table of data to write, `Lusid.PortfolioGroup.Writer` returns an error code. If the operation is successful, the error code is `0`. If unsuccessful, a positive error code and explanation help you discover why LUSID considers the operation invalid.

## Examples

> **Note:** For more example Luminesce SQL queries, visit our [Github repo](https://github.com/finbourne/luminesce-examples/tree/master/examples).

The examples in this section refer to the following portfolio group structure. For simplicity, all the portfolio groups and portfolios are in the same `Ibor` scope; the label in each box represents the code:

![](https://cdn.document360.io/d575ad81-c0ed-4980-bbd1-d59ac5c3de82/Images/Documentation/05c7d7d4-7778-4bb8-ac99-e5a52ff60a0e.png)

### Example 1: Create portfolio groups

This query creates three portfolio groups, without structure. It defaults to an upsert operation as no explicit `WriteAction` is specified.

![](https://cdn.document360.io/d575ad81-c0ed-4980-bbd1-d59ac5c3de82/Images/Documentation/83c1a8ec-300d-4848-94ae-cd262149f67d.png)

```sql
@@scope = select 'Ibor';

@portfolio_groups = values
('Global', 'Global portfolio group', 'A portfolio group representing global operations'),
('NorthAmerica', 'North American portfolio group', 'A portfolio group representing NA operations'),
('EMEA', 'EMEA portfolio group', 'A portfolio group representing NA operations');

@data_to_write = select @@scope as TopGroupScope, column1 as TopGroupCode, 
column2 as TopGroupName, column3 as TopGroupDescription from @portfolio_groups;

select * from Lusid.PortfolioGroup.Writer where ToWrite = @data_to_write;
```

### Example 2: Add sub-groups to group

This query adds `NorthAmerica` and `EMEA` as sub-groups to the `Global` portfolio group:

![](https://cdn.document360.io/d575ad81-c0ed-4980-bbd1-d59ac5c3de82/Images/Documentation/48e2bbd5-5c5d-4dfe-afc2-f50b12bb9a45.png)

```sql
@@scope = select 'Ibor';

@sub_groups = values
('NorthAmerica'),
('EMEA');

@data_to_write = select @@scope as TopGroupScope, 'Global' as TopGroupCode, 
@@scope as BottomGroupScope, column1 as BottomGroupCode, 'Add' as WriteAction from @sub_groups;

select * from Lusid.PortfolioGroup.Writer where ToWrite = @data_to_write;
```

### Example 3: Add portfolios to groups

This query adds (pre-existing) portfolios to the `NorthAmerica` and `EMEA` sub-groups, creating the structure at the top of this section.

```sql
@@scope = select 'Ibor';

@portfolios = values
('Canada', 'NorthAmerica'),
('US', 'NorthAmerica')
('UK', 'EMEA')
('France', 'EMEA')
('Spain', 'EMEA');

@data_to_write = select @@scope as TopGroupScope, column2 as TopGroupCode, 
@@scope as PortfolioScope, column1 as PortfolioCode, 'Add' as WriteAction from @portfolios;

select * from Lusid.PortfolioGroup.Writer where ToWrite = @data_to_write;
```
