The U.S. deficit in transactions with the rest of the world hit a record $41.1 billion in the second quarter, up sharply from $36.8 billion in the first three months of the year, the Commerce Department reported yesterday.

For the first half of the year, the deficit in the so-called current account, which includes trade in goods, investment income and other services, government and private remittances, tourism and military transfers, reached $77.9 billion, up from $61.5 billion in the same period a year ago.

Most of the widening of the current account deficit since last year was the result of a bigger gap between imports and exports of goods, a gap that also reached a record, $39.5 billion, in the second quarter.

But the long-standing U.S. surplus in services trade fell sharply in the second quarter. After averaging about $5 billion a quarter in 1986 and the first quarter of this year, the surplus in services dwindled to $1.3 billion, according to the seasonally adjusted but preliminary figures released yesterday.

The numbers can vary substantially from quarter to quarter and reconciliation of all of the trade and other flows into and out of the United States often leaves a large statistical discrepancy. However, analysts expressed concern about the new evidence that the United States is importing, in one form or another, ever more than it is exporting.

Based on the results for the first half of the year, some analysts raised their forecast of the 1987 current account deficit to between $155 billion and $160 billion. That amount, which must be financed by U.S. borrowing abroad, would compare with a deficit of $141.4 billion last year.

If imports and exports are adjusted for price changes, the U.S. trade deficit has been improving since the third quarter of last year, and hefty increases in exports have helped boost the gross national product since then. However, with the value of the dollar declining and prices of imports rising much faster than prices of exports, the current account deficit is still going up. And that is what must be financed each year by foreign private or government capital inflows to the United States.

With the added indebtedness taken on by U.S. citizens to cover the $41.1 billion current account deficit in the second quarter, the United States now has about $340 billion less invested or lent abroad than foreigners have invested or lent here. As recently as five years ago, in the second quarter of 1982, the United States still had a small current account surplus and a significant net foreign investment position.

As that investment position has deteriorated to the point that the United States is now by far the world's largest debtor, its advantage in earnings on its investments has also begun to shrink. In the second quarter, the U.S. surplus on such investment earnings declined to $1.6 billion, down from $5.5 billion in the first quarter. The surplus on investment income was $20.8 billion for all of last year.

Part of the decline in the investment income surplus has been masked since 1985 by the rapid decline of the dollar. When the dollar falls relative to the West German mark, for example, earnings by Opel, the German auto subsidiary of General Motors Corp., go up when they are translated into dollars -- even if they are unchanged in marks, according to Commerce statisticians.

Since returns on foreign investments and loans in the United States are reported in dollars in the first place, there is no offsetting adjustment on the other side of the ledger.

As for merchandise trade, Commerce also reported yesterday that when both imports and exports are valued so that the cost of insurance and shipping are excluded, the monthly trade deficit in July was $14.8 billion, up from $14.1 billion in June.

Last week, Commerce said the deficit was $16.5 billion when insurance and freight charges are included in imports but not exports. The department is legally required to delay for two business days release of the import values calculated on the same basis as exports, which tends to inflate the deficit.